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")
}
}
swift 设置导航栏(基础篇)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 第一讲开始! 万事开头难,项目结构框架搭建是整个APP开发的头,是核心,是地基,只有结构搭建完成才能继续后面的工作...
- self.navigationController!.navigationBar.setBackgroundIma...