您的当前位置:首页UINavigationBar Tips

UINavigationBar Tips

2024-12-12 来源:哗拓教育
  • 设置所有导航栏的属性:
let allNavigationBar = UINavigationBar.appearance()
//设置allNavigationBar的属性...
  • 更改导航栏的背景颜色:
navigationBar.barTintColor = UIColor.blackColor()
  • 更改导航栏的文字颜色:
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
  • 更改导航栏的标题字体及颜色:
if let barFont = UIFont(name: "Avenir-Light", size: 24.0)  {
            
    navigationBar.titleTextAttributes = [
        NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSFontAttributeName : barFont
    ]
}
  • 更改返回按钮文字
    例如: 页面1条转到页面2,更改页面2返回按钮的标题,把下面代码加入页面1的viewDidLoad方法中
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
  • 去掉更改全部导航的返回按钮文字
let allBarButtonItem = UIBarButtonItem.appearance()
  allBarButtonItem.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -60), for: .default)
  • 更改返回按钮颜色
navigationBar.tintColor = UIColor.white
  • 取消半透明:
navigationBar.isTranslucent = false
  • 设置主界面隐藏navigationBar,跳转后显示出来
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.setNavigationBarHidden(true, animated: false)
}
    
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    navigationController?.setNavigationBarHidden(false, animated: false)
}
  • 禁用侧滑手势
 navigationController?.interactivePopGestureRecognizer?.isEnabled = false
显示全文