Swift使标签重叠状态栏

时间:2017-02-17 06:31:17

标签: swift

我在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个字符时显示。现在,当我显示状态栏与输入重叠时,我该如何防止它?

这就是它的样子

enter image description here

1 个答案:

答案 0 :(得分:1)

  1. 设置标签的位置。
  2. 将标签添加到视图的子视图后。您需要使用以下约束来设置它的位置:

    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

    1. 仅在用户输入少于6个字符时显示
    2. 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
      }