swift 設定導覽列(基礎篇)

來源:互聯網
上載者:User
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")    }}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.