Navigation bar 的注意事项

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

需要设置 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)

如果先设置 isEnabled,后设置 bar button item

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

结果 isEnabled 还是 true

正确的顺序

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

结果 isEnabled 是 false

改变 navigation bar isTranslucent 属性会改变 view 的坐标

放置两个 label。其中, frameLabel 没有添加约束(NSLayoutConstraint),constraintLabel 左、右、下都有约束,与 view 相接。

设置右上角按钮动作

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Change", style: .plain, target: self, action: #selector(rightButtonClicked(_:)))

改变 navigation bar isTranslucent 属性,显示 label 的坐标

@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 坐标改变,Status bar 和 navigation bar 的坐标不变

Navigation bar 从不透明变透明,status bar 和 navigation bar 的坐标都不变。整个 view 下移64,高度减小64,不会超出 window。没加约束的 frameLabel 坐标不变,但相对 window 的位置随着 view 一起下移。添加约束的 constraintLabel 的坐标改变,但是相对 window 的位置不变。

如果需要改变 navigation bar isTranslucent 属性,就要考虑对其他 view 会不会有影响,是否使用约束来定位。

转载请注明出处:http://www.cnblogs.com/silence-cnblogs/p/6262471.html

Navigation bar 的注意事项的更多相关文章

  1. android隐藏底部虚拟键Navigation Bar实现全屏

    隐藏底部虚拟键Navigation Bar实现全屏有两种情况 第一种:始终隐藏,触摸屏幕时也不出现 解决办法:同时设置以下两个参数 View.SYSTEM_UI_FLAG_HIDE_NAVIGATIO ...

  2. 非常不错的Navigation Bar效果

    非常不错的Navigation Bar效果,该源码转载于源码天堂的,大家可以了解一下吧,多视图应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置. 源码我就不上传了,大家 ...

  3. HTML5设计网页熔岩灯导航(navigation bar)插件 已经加上完整源代码

    导航栏(navigation bar): 1.指位于页眉区域的,在页眉横幅图片上边或下边的一排水平导航按钮,它起着链接博客的各个页面的作用. 2.网页设计中不可缺少的部分,它是指通过一定的技术手段,为 ...

  4. 与Status Bar和Navigation Bar相关的一些东西

    Android Navigation Bar Status Bar   与StatusBar和NavigationBar相关的东西有两种,一是控制它们的显示与隐藏,二是控制它们的透明与否及背景. 在2 ...

  5. Customizing Navigation Bar and Status Bar

    Like many of you, I have been very busy upgrading my apps to make them fit for iOS 7. The latest ver ...

  6. 配置navigation bar外观

    /* 配置navigation bar外观开始 */ self.navigationBar.translucent = YES; self.navigationBar.titleTextAttribu ...

  7. [工作积累] Android: Hide Navigation bar 隐藏导航条

    https://developer.android.com/training/system-ui/navigation.html View decorView = getWindow().getDec ...

  8. nested pop animation can result in corrupted navigation bar

    nested pop animation can result in corrupted navigation barFinishing up a navigation transition in a ...

  9. nested push animation can result in corrupted navigation bar

    2013-12-06 10:15:51.668 CodingForFun[4569:70b] nested push animation can result in corrupted navigat ...

随机推荐

  1. requestFocusFromTouch , requestFocus

    /*if(userNameEditText.isInTouchMode()){ userNameEditText.requestFocusFromTouch(); }else{ userNameEdi ...

  2. oralce表空间自增长占满磁盘

    取消表空间自动增长 SELECT FILE_NAME,TABLESPACE_NAME,AUTOEXTENSIBLE,bytes/1024/1024/1024 FROM dba_data_files; ...

  3. 转:XMLP报表导出为excel时设置文本不自动转为数字

    转自:http://yedward.net/?id=337 对于这个问题,只要在RTF模版中设置下强制LTR即可,设置方法如下: 图1:勾选强制LTR 也可以自己输入下面的代码: <fo:bid ...

  4. 实现一个脚本语言Raven(一)

    之前实现了Raven语言的0.1版,仅仅支持表达式处理与控制语句,由于不支持数组.函数.类,甚至都不是图灵完全的语言. 现在参考vczh的博客打算重新写一遍Raven语言.陈祖不愧是神啊,高中就写出支 ...

  5. 单台电脑上启动多个Modelsim图形环境窗口的简单办法(windows)

    1 单台电脑上启动多个Modelsim图形环境窗口的简单办法(windows) http://blog.21ic.com/user1/3128/archives/2010/73447.html   单 ...

  6. PHP Fatal error问题处理

    今天一个朋友公司网站遇到一个关于PHP的问题: PHP Fatal error:  Allowed memory size of 67108864 bytes exhausted (tried to ...

  7. 你不知道的javascript 上卷 读书笔记

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 关于ASPOSE.WORD使用上的一个小问题

    最近实习期间负责了公司某个项目的一个功能模块里面的word导出功能,使用的是ASPOSE.WORD类库,但是经常导出时候会遇到图中的问题,大概意思就是两个表格不能跨在一起,调试了好几次还是没发现具体的 ...

  9. Sidebar Enhancements使用说明

    SideBarEnhancements是Sublime侧栏文件与文件夹的增强工具 Version: 2015.5.27 通过使用Sumbime Text3安装包工具(Package Control,快 ...

  10. 如何诊断oracle数据库运行缓慢或hang住的问题

    为了诊断oracle运行缓慢的问题首先要决定收集哪些论断信息,可以采取下面的诊断方法:1.数据库运行缓慢这个问题是常见还是在特定时间出现如果数据库运行缓慢是一个常见的问题那么可以在问题出现的时候收集这 ...