我在 Swift 中有这个标签
let top_error_rep: UILabel = {
let lb = UILabel()
lb.text="Password should be at least 6 charachters"
lb.textColor = UIColor(r: 230, g: 230, b: 230)
lb.backgroundColor = .red
return lb;
}()
我只在用户输入少于 6 个字符时显示它。现在,当我显示状态栏与输入重叠时,我该如何防止这种情况发生?
原来是这样的
最佳答案
- 设置标签的位置。
将标签添加到 View 的 subview 之后。您需要使用这样的约束来设置它的位置:
top_error_rep.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
top_error_rep.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
top_error_rep.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
top_error_rep.heightAnchor.constraint(equalToConstant: 20).isActive = true
或者您可以使用不同的方式。 Google for 以编程方式快速自动布局
- 仅在用户输入少于 6 个字符时显示
How do I check when a UITextField changes?
这可能对你有帮助
编辑
将此添加到您的 ViewController 以隐藏状态栏
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//Add below line.....
UIApplication.shared.isStatusBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
//It will show the status bar again after dismiss
UIApplication.shared.isStatusBarHidden = false
}
override var prefersStatusBarHidden: Bool {
return true
}
关于Swift 使标签重叠状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42290887/