Swift 使标签重叠状态栏

标签 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. 设置标签的位置。

将标签添加到 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 以编程方式快速自动布局

  1. 仅在用户输入少于 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/

相关文章:

ios - 我想在导航栏上设置后退按钮文本,但我不能。此代码不起作用

Swift 将 Plist(NSString 内)转换为 NSDictionary

在 guard 语句中快速使用 break

ios - Storyboard View (iphone 4/5/6)影响集合单元格大小的布局(以编程方式设置)

swift - 只要用户点击按钮,我就想更改按钮的图像?

xcode - 我应该使用什么函数来调出 CVCalendar 中的完整菜单?

ios - 带有约束的标签添加到具有纵横比的 ImageView

swift - RxSwift - 与 Singleton 不同类型的队列可观察值

ios - 弹出错误位置

ios - 如何在 iOS Swift 中设置延迟?