如何在 Swift 中舍入 UITextField 的右上角和左下角
How to round the UITextField topRight and bottomLeft corners in Swift
我想圆角 UITextField
topLeft 和 bottomRight。
func conerRounderTextField(sides: CACornerMask) -> UITextField {
let tf = UITextField()
tf.backgroundColor = .white
tf.clipsToBounds = true
tf.layer.cornerRadius = 20
tf.layer.maskedCorners = sides
tf.layer.borderWidth = 1
tf.layer.borderColor = UIColor.gray.cgColor
return tf
}
并像这样调用 TextField:
private let passwordTextField: UITextField = {
let tf = Utillities().conerRounderTextField(sides: [.layerMaxXMinYCorner, .layerMinXMaxYCorner])
return tf
}()
通过使用它,我得到了我想要的设计,但 TextField 不工作。
你能帮帮我吗?
以下对我有效(Xcode 11.5):
class ViewController: UIViewController {
func cornerRounderTextField(sides: CACornerMask) -> UITextField {
let tf = UITextField()
tf.backgroundColor = .white
tf.clipsToBounds = true
tf.layer.cornerRadius = 16
tf.layer.maskedCorners = sides
tf.layer.borderWidth = 1
tf.layer.borderColor = UIColor.gray.cgColor
return tf
}
private lazy var passwordTextField: UITextField = {
let tf = cornerRounderTextField(sides: [.layerMaxXMinYCorner, .layerMinXMaxYCorner])
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(passwordTextField)
NSLayoutConstraint.activate([
passwordTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
passwordTextField.centerYAnchor.constraint(equalTo: view.centerYAnchor),
passwordTextField.widthAnchor.constraint(equalToConstant: 200),
passwordTextField.heightAnchor.constraint(equalToConstant: 30)
])
}
}
(您可以使用 cmd + k 切换键盘)
我想圆角 UITextField
topLeft 和 bottomRight。
func conerRounderTextField(sides: CACornerMask) -> UITextField {
let tf = UITextField()
tf.backgroundColor = .white
tf.clipsToBounds = true
tf.layer.cornerRadius = 20
tf.layer.maskedCorners = sides
tf.layer.borderWidth = 1
tf.layer.borderColor = UIColor.gray.cgColor
return tf
}
并像这样调用 TextField:
private let passwordTextField: UITextField = {
let tf = Utillities().conerRounderTextField(sides: [.layerMaxXMinYCorner, .layerMinXMaxYCorner])
return tf
}()
通过使用它,我得到了我想要的设计,但 TextField 不工作。
你能帮帮我吗?
以下对我有效(Xcode 11.5):
class ViewController: UIViewController {
func cornerRounderTextField(sides: CACornerMask) -> UITextField {
let tf = UITextField()
tf.backgroundColor = .white
tf.clipsToBounds = true
tf.layer.cornerRadius = 16
tf.layer.maskedCorners = sides
tf.layer.borderWidth = 1
tf.layer.borderColor = UIColor.gray.cgColor
return tf
}
private lazy var passwordTextField: UITextField = {
let tf = cornerRounderTextField(sides: [.layerMaxXMinYCorner, .layerMinXMaxYCorner])
tf.translatesAutoresizingMaskIntoConstraints = false
return tf
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(passwordTextField)
NSLayoutConstraint.activate([
passwordTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
passwordTextField.centerYAnchor.constraint(equalTo: view.centerYAnchor),
passwordTextField.widthAnchor.constraint(equalToConstant: 200),
passwordTextField.heightAnchor.constraint(equalToConstant: 30)
])
}
}
(您可以使用 cmd + k 切换键盘)