2017-02-17 40 views
1

我有這樣的標籤在斯威夫特雨燕化妝標籤重疊狀態欄

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

+0

你在哪裏放置標籤? –

+0

在UIview的頂部@GaneshKumar –

+0

提供的代碼和截圖如何出現 –

回答

1
  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 

或者您可以使用不同的方式。谷歌爲Auto layout programmatically swift

  • 使當用戶輸入少於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 
    } 
    
    +0

    我已經添加了約束。但我只需要我的標籤重疊狀態欄 –

    +0

    抱歉誤會您的問題...我編輯我的回答 –

    +0

    謝謝你的回答! –