全局修改导航栏的背景色、按键颜色、标题颜色,在 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)
标题:
// 修改当前标题文本 // 方法1self.title = "HaHa"// 方法2 (不建议使用)let titleLabel = UILabel(frame: CGRectMake(0, 0, view.frame.size.width - 120, 44))titleLabel.textAlignment = NSTextAlignment.CentertitleLabel.text = "Title"self.navigationItem.titleView = titleLabel// orself.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()
联系客服