我在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个字符时显示。现在,当我显示状态栏与输入重叠时,我该如何防止它?
这就是它的样子
答案 0 :(得分:1)
将标签添加到视图的子视图后。您需要使用以下约束来设置它的位置:
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为Auto layout programmatically swift
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
}