微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Swift中将UITextField的topRight和bottomLeft角取整

如何解决如何在Swift中将UITextField的topRight和bottomLeft角取整

我想绕过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无法正常工作。

design

你能帮我吗?

解决方法

以下对我有用(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切换键盘)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。