溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-08-18 11:15:20 來源:億速云 閱讀:144 作者:小新 欄目:移動(dòng)開發(fā)

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

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ò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI