包括一些顶部栏的基操、以及自定义的和HZNavigationBar的pod库的使用
反正也是瞎写写啦~︿( ̄︶ ̄)︿
顶部栏的一些设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .cyan navigationItem.title = "知乎日报" navigationItem.prompt = "正在使用您的位置..." navigationController?.navigationBar.isTranslucent = false navigationController?.navigationBar.barStyle = UIBarStyle.black navigationController?.navigationBar.tintColor = UIColor.white navigationController?.navigationBar.setBackgroundImage(UIImage(named:""), for: .any, barMetrics: .default) navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18)] navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white] navigationController?.navigationBar.shadowImage = UIImage(named:"") navigationController?.navigationBar.layer.shadowColor = UIColor.green.cgColor navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 10) navigationController?.navigationBar.layer.shadowOpacity = 0.2
} override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(true, animated: false) }
|
这里如果想要设置Title的颜色,参考https://www.crifan.com/swift_set_navigationbar_title_text_color/,渐变色炫酷状态栏(diss by Jason: 被设计打死)https://www.crifan.com/swift_set_navigationbar_title_text_color/
使用titleView和customView
这两个可以模仿UI控件的外形,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| let title = UILabel() title.text = "知乎日报" title.textColor = .darkGray title.font = UIFont.systemFont(ofSize: 28, weight: .heavy) title.textAlignment = .left title.sizeToFit() navigationItem.titleView = title let date = Date.init() let timeFormatter = DateFormatter.init() timeFormatter.dateFormat="MM月\ndd日" let time = timeFormatter.string(from: date) let dateLabel = UILabel() dateLabel.text = time dateLabel.textColor = .black dateLabel.numberOfLines = 0 dateLabel.frame = CGRect(x: 20, y: 200, width: 100, height: 60) navigationItem.leftBarButtonItem = UIBarButtonItem(customView: dateLabel)
|
HZNavigationBar
老方法在Podfile中添加pod 'HZNavigationBar’然后install
基础设置看这里
让 NavigationBar 变透明
1 2 3
| self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true
|
导航栏样式修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| *// *修改导航背景色 self.navigationController?.navigationBar.barTintColor = UIColor.blackColor() *// *修改导航栏文字颜色 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.whiteColor()] *// *修改导航栏按钮颜色 self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() *// *修改导航背景图片* *不包含状态栏:*44*点(*88*像素)* *包含状态栏:*64*点*(128*像素) self.navigationController?.navigationBar .setBackgroundImage(UIImage(named: "bg"), forBarMetrics: .Default) navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) navigationController?.navigationBar.shadowImage = UIImage()
|
参考:UINavigationController的常用属性
修改导航栏“返回”按钮
1 2
| self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: nil)
|
更多 例如 在子页面(栈)添加不同的顶部栏 访问https://www.hangge.com/blog/cache/detail_957.html