Swift5.0|Swift5.0 导航栏的基本使用

self.navigationItem.title = "设置本页面的导航栏" self.navigationController?.navigationBar.isTranslucent= false //设置导航栏系统默认按钮图标和文字的颜色 self.navigationController?.navigationBar.tintColor = .white // //导航栏的背景颜色 self.navigationController?.navigationBar.barTintColor = .orange //设置导航栏标题文字的颜色 self.navigationController?.navigationBar.titleTextAttributes =[NSAttributedString.Key.foregroundColor: UIColor.white]//设置导航栏文字的其他样式//self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font:UIFont.italicSystemFont(ofSize: 18),NSAttributedString.Key.backgroundColor:UIColor.purple]//设置导航栏的背景颜色 self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "gougou.jpg"), for: .default) // 设置导航栏背景图片 let imagegougou = UIImage.init(named: "导航栏@2x") self.navigationController?.navigationBar.setBackgroundImage(imagegougou, for: .default)// 设置(去掉)导航栏底部线条 self.navigationController?.navigationBar.shadowImage = UIImage()//第一种自定义返回按钮 let leftButton = UIButton.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))leftButton.backgroundColor = .red leftButton.setImage(UIImage.init(named: "backWhit(gray)"), for: .normal) leftButton.setTitleColor(.white, for: .normal) leftButton.titleLabel?.font = UIFont.init(name: "Helvetica-Bold", size: 15) //leftButton.setTitle("返回", for: .normal) leftButton.addTarget(self, action: #selector(backClick), for: .touchUpInside) self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)//另外一种方式设置导航栏图片按钮 ///下面2行代码作用是不让按钮的图标给渲染成系统蓝色的 var leftImage = UIImage.init(named: "backWhit(gray)") leftImage = leftImage!.withRenderingMode(.alwaysOriginal)self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: leftImage, style: .plain, target: self, action: #selector(leftButtonClick))//返回按钮方法的实现 @objc func backClick() {self.navigationController?.popViewController(animated: true)}@objc func leftButtonClick () { self.navigationController?.popViewController(animated: true)}

    推荐阅读