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")    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.