微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

UIKit – UIBarButtonItem(导航按钮)

ui aide_941 26℃

UIKit – UIBarButtonItem(导航按钮)

UIBarButtonItem继承于UIBarItem,用于设置导航栏上的按钮组。

1.显示与隐藏回退按钮

  1. self.navigationItem.hidesBackButton = true
  2. self.navigationItem.setHidesBackButton(true, animated: true)

2.创建文字按钮

  1. //左侧按钮
  2. let leftButton = UIBarButtonItem(title: “Back”, style: .plain, target: self, action: #selector(FirstPageViewController.prevButton))
  3. self.navigationItem.leftBarButtonItem = leftButton
  4. //右侧按钮
  5. let rightButton = UIBarButtonItem(title: “Next”, style: .plain, target: self, action: #selector(FirstPageViewController.nextButton))
  6. self.navigationItem.rightBarButtonItem = rightButton

3.创建系统自带按钮

  1. let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
  2. let cameraButton = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: nil)
  3. let editButton = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: nil)
  4. //同时添加多个按钮
  5. self.navigationItem.rightBarButtonItems = [addButton,cameraButton,editButton]

4.添加自定义按钮

  1. //自定义搜索框
  2. let search = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 25))
  3. search.backgroundColor = UIColor.gray
  4. search.placeholder = “请输入搜索内容”
  5. search.layer.masksToBounds = true
  6. search.layer.cornerRadius = 8
  7. search.layer.borderWidth = 2
  8. search.layer.borderColor = UIColor.blue.cgColor
  9. let itemButton = UIBarButtonItem(customView: search)
  10. //文本
  11. let txt = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 25))
  12. txt.text = “搜索”
  13. let txtButton = UIBarButtonItem(customView: txt)
  14. //添加按钮组
  15. self.navigationItem.rightBarButtonItems = [itemButton, txtButton]

5.按钮样式

  1. let leftButton = UIBarButtonItem(title: “Back”, style: .plain, target: self, action: #selector(FirstPageViewController.prevButton))
  2. //字体颜色
  3. leftButton.tintColor = UIColor.red
  4. //按钮背景图片
  5. leftButton.setBackgroundImage(UIImage(named:“image”), for: .normal, barMetrics: .default)
  6. self.navigationItem.leftBarButtonItem = leftButton

效果如下图:

Caption

转载请注明:SuperIT » UIKit – UIBarButtonItem(导航按钮)

喜欢 (0)or分享 (0)