- 全局修改导航栏的背景色、按键颜色、标题颜色,在 AppDelegate.swift 中 添加(只有放在这里有效):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Bar's background color
UINavigationBar.appearance().barTintColor = UIColor.grayColor()
// Back button and such
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
// Title's text color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.redColor()]
return true
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController?.navigationBar.shadowImage = UIImage()
}
- 定义子页面返回键的文字文本,不显示设置为""就可:
// 在主界面中
navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
// 修改当前标题文本
// 方法1
self.title = "HaHa"
// 方法2 (不建议使用)
let titleLabel = UILabel(frame: CGRectMake(0, 0, view.frame.size.width - 120, 44))
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.text = "Title"
self.navigationItem.titleView = titleLabel
// or
self.navigationController?.navigationBar.topItem?.titleView = titleLabel
// 修改当前标题颜色
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationItem.rightBarButtonItem?.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.greenColor()], forState: UIControlState.Disabled)
self.navigationItem.rightBarButtonItem?.enabled = false
// 在右侧添加一个按钮
let barButtonItem = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "tapBarButton")
self.navigationItem.rightBarButtonItem = barButtonItem
// 往右侧添加多个按钮
let item1 = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Plain, target: self, action: "tapBarButton")
let item2 = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Plain, target: self, action: "tapBarButton")
// 第一个在最右侧
self.navigationItem.rightBarButtonItems = [item1, item2]
- 解决 Push ViewController 过程中右上角有黑影的问题
self.navigationController?.view.backgroundColor = UIColor.whiteColor()