Navigation中bar注意事項(xiàng)有哪些

這篇文章給大家分享的是有關(guān)Navigation中bar注意事項(xiàng)有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比萊山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式萊山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋萊山地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴。

Bar button item 使用 button 作為 custom view,初始化 isEnabled 為 false,注意順序

需要設(shè)置 bar button item 的 custom view 為 button,但一開始 isEnabled 要為 false。

生成一個(gè) 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

Navigation中bar注意事項(xiàng)有哪些

正確的順序

navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
leftButton.isEnabled = false // or navigationItem.leftBarButtonItem?.isEnabled = false

結(jié)果 isEnabled 是 false

Navigation中bar注意事項(xiàng)有哪些

改變 navigation bar isTranslucent 屬性會(huì)改變 view 的坐標(biāo)

放置兩個(gè) label。其中, frameLabel 沒有添加約束(NSLayoutConstraint),constraintLabel 左、右、下都有約束,與 view 相接。

Navigation中bar注意事項(xiàng)有哪些

設(shè)置右上角按鈕動(dòng)作

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)
}

通過點(diǎn)擊右上角按鈕,來查看變化。

透明時(shí)

Navigation中bar注意事項(xiàng)有哪些

不透明時(shí)

Navigation中bar注意事項(xiàng)有哪些

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

Navigation中bar注意事項(xiàng)有哪些

Navigation bar 從不透明變透明,status bar 和 navigation bar 的坐標(biāo)都不變。整個(gè) view 下移64,高度減小64,不會(huì)超出 window。沒加約束的 frameLabel 坐標(biāo)不變,但相對(duì) window 的位置隨著 view 一起下移。添加約束的 constraintLabel 的坐標(biāo)改變,但是相對(duì) window 的位置不變。

如果需要改變 navigation bar isTranslucent 屬性,就要考慮對(duì)其他 view 會(huì)不會(huì)有影響,是否使用約束來定位。

感謝各位的閱讀!關(guān)于“Navigation中bar注意事項(xiàng)有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享標(biāo)題:Navigation中bar注意事項(xiàng)有哪些
本文網(wǎng)址:http://muchs.cn/article42/ihgdec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、虛擬主機(jī)、商城網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司