ios - 自定义文本字段不符合约束

标签 ios swift uitextfield constraints

我一直遇到这个问题 HoshiTextField很长一段时间以来,我也在 git 上提出了一个问题,但没有得到答案......

这就是它应该的样子,以及如果用户选择 textField 时的样子。 :

enter image description here

设置textField时出现问题至becomeFirstResponder里面viewDidLoad或当 popping一个ViewControllertextField第一个里面VC被选中。显然这搞乱了 framesconstraints textField的但我完全不知道如何解决这个问题。

enter image description here

正如您所看到的,“Email-Adesse”文本正在移动到左上角,并且当 pushing 时和poping返回ViewController它甚至超出了 constraints 。检查View Hirarchy时使用调试器,“电子邮件地址”文本看起来完美到位,即使事实并非如此。设置 viewDidAppear 内的约束没有改变任何东西。

我限制 textFields像所有其他元素一样:

let emailTextField: HoshiTextField = {
    let v = HoshiTextField()
    v.borderActiveColor = .white
    v.borderInactiveColor = .white
    v.textColor = .white
    v.font = UIFont(name: "AvenirNext-Regular", size: 17)
    v.placeholder = "Email-Adresse"
    v.placeholderColor = .white
    v.placeholderFontScale = 0.8
    v.minimumFontSize = 13
    v.borderStyle = .line
    v.autocapitalizationType = .none
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()
emailTextField.topAnchor.constraint(equalTo: theLabel.bottomAnchor, constant: 20).isActive = true
    emailTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30).isActive = true
    emailTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
    emailTextField.heightAnchor.constraint(equalToConstant: 60).isActive = true

如果有人能帮助我,我将不胜感激!希望问题清楚了,你也可以看看我的项目,自己看看问题:

Git repo to my project

最佳答案

这是完整的测试 Controller ,其中包含 2 个可能的解决方案替代方案

演示:备用 1 - 通常显示已展开

demo

演示:Alternate 2 - 总是有延迟,扩展可见

demo2

class ViewController2: UIViewController {
    @IBOutlet weak var theLabel: UILabel!
    private weak var emailTextField: HoshiTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        emailTextField = {
            let v = HoshiTextField()
            v.borderActiveColor = .white
            v.borderInactiveColor = .white
            v.textColor = .white
            v.font = UIFont(name: "AvenirNext-Regular", size: 17)
            v.placeholder = "Email-Adresse"
            v.placeholderColor = .white
            v.placeholderFontScale = 0.8
            v.minimumFontSize = 13
            v.borderStyle = .line
            v.autocapitalizationType = .none
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()
        self.view.addSubview(emailTextField)
        emailTextField.topAnchor.constraint(equalTo: theLabel.bottomAnchor, constant: 20).isActive = true
            emailTextField.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 30).isActive = true
            emailTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
            emailTextField.heightAnchor.constraint(equalToConstant: 60).isActive = true

//        DispatchQueue.main.async {
//            self.emailTextField.becomeFirstResponder()    // Alternate 1
//        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        emailTextField.becomeFirstResponder()          // Alternate 2
    }
}

关于ios - 自定义文本字段不符合约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60670915/

相关文章:

ios - 为 Firebase 6.5.0 安装 Geofirestore

ios - 从 api 检索大量数据并在 UICollectionView Swift 中使用它

java - 在 macOS Cocoa 应用程序中嵌入可执行 jar 文件

ios - 如何在文本字段 swift ios 中输入 4 个字母后添加功能

ios - 立即用 UIPickerView 选项填充 UITextField

ios - 在RxSwift单元测试中模拟和验证结果

ios - 比较 Cloudkit 上的订阅

swift - 在 macOS Big Sur 下使用交替行颜色的 NSTableView 的视觉故障

ios - Swift 运行错误

ios - 密码文本字段的占位符文本显示为密码而不是文本