extension HomeViewController{ private func setupNavigationBar(){ /*** 設定導覽列背景顏色 ***/ navigationController?.navigationBar.barTintColor = UIColor.red /*** 設定導覽列title ***/ // self.title = "網易新聞" navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white] // 自訂view設定title let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) titleLabel.text = "網易新聞" titleLabel.textColor = UIColor.white navigationItem.titleView = titleLabel // 圖片 let imageView = UIImageView(image: UIImage(named : "contentview_imagebg_logo")) navigationItem.titleView = imageView /*** 設定導覽列左右按鈕 ***/ //文字 let leftBarButtonItem = UIBarButtonItem(title: "leftButton", style: .plain, target: self, action: #selector(self.leftClick)) let rightBarButtonItem = UIBarButtonItem(title: "rightButton", style: .plain, target: self, action: #selector(self.rightClick)) navigationItem.leftBarButtonItem = leftBarButtonItem navigationItem.rightBarButtonItem = rightBarButtonItem // 圖片 navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "fog"), style: .plain, target: self, action: #selector(self.leftClick)) navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "fog"), style: .plain, target: self, action: #selector(self.rightClick)) // 自訂 let leftButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) let rightButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) leftButton.setTitleColor(UIColor.black, for: UIControlState.normal) rightButton.setTitleColor(UIColor.black, for: UIControlState.normal) leftButton.setTitle("leftButton", for: UIControlState.normal) rightButton.setTitle("rightButton", for: UIControlState.normal) leftButton.addTarget(self, action: #selector(leftClick), for: UIControlEvents.touchUpInside) rightButton.addTarget(self, action: #selector(rightClick), for: UIControlEvents.touchUpInside) navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton) navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton) // BarButtonItem顏色 leftBarButtonItem.tintColor = UIColor.black } @objc private func leftClick() { print("leftClick") } @objc private func rightClick() { print("rightClick") }}