Navigation bar的注意事項詳解
Bar button item 使用 button 作為 custom view,初始化 isEnabled 為 false,注意順序
需要設(shè)置 bar button item 的 custom view 為 button,但一開始 isEnabled 要為 false。
生成一個 button
let leftButton = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 44))
leftButton.setTitleColor(UIColor.green, for: .normal)
leftButton.setTitleColor(UIColor.red, for: .disabled)
leftButton.setTitle("Enabled", for: .normal)
leftButton.setTitle("Disabled", for: .disabled)
leftButton.addTarget(self, action: #selector(leftButtonClicked(_:)), for: .touchUpInside)
如果先設(shè)置 isEnabled,后設(shè)置 bar button item
leftButton.isEnabled = false navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
結(jié)果 isEnabled 還是 true

正確的順序
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton) leftButton.isEnabled = false // or navigationItem.leftBarButtonItem?.isEnabled = false
結(jié)果 isEnabled 是 false

改變 navigation bar isTranslucent 屬性會改變 view 的坐標(biāo)
放置兩個 label。其中, frameLabel 沒有添加約束(NSLayoutConstraint),constraintLabel 左、右、下都有約束,與 view 相接。

設(shè)置右上角按鈕動作
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Change", style: .plain, target: self, action: #selector(rightButtonClicked(_:)))
改變 navigation bar isTranslucent 屬性,顯示 label 的坐標(biāo)
@objc private func rightButtonClicked(_ sender: AnyObject) {
navigationController?.navigationBar.isTranslucent = !navigationController!.navigationBar.isTranslucent
updateLabelContent()
}
private func updateLabelContent() {
title = navigationController!.navigationBar.isTranslucent ? "Translecent" : "Opaque"
let frameLabelOrigin = frameLabel.frame.origin
frameLabel.text = "Frame label. x = \(frameLabelOrigin.x), y = \(frameLabelOrigin.y)"
let constraintLabelOrigin = constraintLabel.frame.origin
constraintLabel.text = "Constraint label. x = \(constraintLabelOrigin.x), y = \(constraintLabelOrigin.y)"
print("\(title)")
print("Status bar frame:", UIApplication.shared.statusBarFrame) // (0.0, 0.0, 375.0, 20.0)
print("Navigation bar frame:", navigationController!.navigationBar.frame) // (0.0, 20.0, 375.0, 44.0)
}
通過點擊右上角按鈕,來查看變化。
透明時

不透明時

View controller 的 view 坐標(biāo)改變,Status bar 和 navigation bar 的坐標(biāo)不變

Navigation bar 從不透明變透明,status bar 和 navigation bar 的坐標(biāo)都不變。整個 view 下移64,高度減小64,不會超出 window。沒加約束的 frameLabel 坐標(biāo)不變,但相對 window 的位置隨著 view 一起下移。添加約束的 constraintLabel 的坐標(biāo)改變,但是相對 window 的位置不變。
如果需要改變 navigation bar isTranslucent 屬性,就要考慮對其他 view 會不會有影響,是否使用約束來定位。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
總結(jié)IOS關(guān)閉鍵盤/退出鍵盤的五種方式
IOS開發(fā)中經(jīng)常要用到輸入框,默認情況下點擊輸入框就會彈出鍵盤,但是必須要實現(xiàn)輸入框return的委托方法才能取消鍵盤的顯示,對于用戶體驗來說很不友好,我們可以實現(xiàn)例如點擊鍵盤以外的空白區(qū)域來將鍵盤關(guān)閉的功能,以下是我總結(jié)出的幾種關(guān)閉鍵盤的方法。2016-08-08
iOS實現(xiàn)播放遠程網(wǎng)絡(luò)音樂的核心技術(shù)點總結(jié)
本篇文章主要介紹了iOS播放遠程網(wǎng)絡(luò)音樂的核心技術(shù),采用ios系統(tǒng)自帶的AVFoundation框架來實現(xiàn),有需要的朋友可以了解一下。2016-11-11
iOS中containsString和rangeOfString的區(qū)別小結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于iOS中containsString和rangeOfString的一些區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
全面解析iOS應(yīng)用中自定義UITableViewCell的方法
這篇文章主要介紹了iOS應(yīng)用開發(fā)中自定義UITableViewCell的方法,示例為傳統(tǒng)的Obejective-C語言,需要的朋友可以參考下2016-04-04

