- 增加了类型为 UINavigationItem.ItemStyle 的属性
style
用于描述 UINavigationItem 在 UINavigationBar 上的布局,共有 3 种样式:navigator
(默认样式),browser
(浏览器样式)与editor
(编辑器样式)。
代码语言:javascript
复制// Created by YungFan
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
// 左侧
navigationItem.leftBarButtonItems = [
UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(gotoNextViewController)),
UIBarButtonItem(barButtonSystemItem: .bookmarks, target: nil, action: nil)]
// 标题
navigationItem.title = "导航栏"
// navigationItem.titleView = UISwitch()
// 中间
let fixedGroup = UIBarButtonItemGroup.fixedGroup(items: [
UIBarButtonItem(title: "撤销", image: UIImage(systemName: "arrow.uturn.backward"),
primaryAction: UIAction { _ in
}),
UIBarButtonItem(title: "反撤销", image: UIImage(systemName: "arrow.uturn.forward"),
primaryAction: UIAction { _ in
})])
let movableGroup = UIBarButtonItem(title: "裁剪", image: UIImage(systemName: "crop"),
primaryAction: UIAction { _ in
}).creatingMovableGroup(customizationIdentifier: "Cropping")
navigationItem.centerItemGroups = [fixedGroup, movableGroup]
// 右侧
navigationItem.rightBarButtonItems = [
UIBarButtonItem(barButtonSystemItem: .camera, target: nil, action: nil),
UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: nil)]
// 设置style
navigationItem.style = .editor
}
// MARK: 导航跳转
@objc func gotoNextViewController(_ sender: UIBarButtonItem) {
let nextViewController = UIViewController()
nextViewController.view.backgroundColor = .red
nextViewController.navigationItem.title = "下一页"
navigationController?.pushViewController(nextViewController, animated: true)
}
}
- 增加了闭包类型的属性
titleMenuProvider
用于给当前导航栏的标题增加操作菜单。
代码语言:javascript
复制// Created by YungFan
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "标题"
// 设置titleMenuProvider
navigationItem.titleMenuProvider = { _ in
let favorite = UIAction(title: "Favorite", image: UIImage(systemName: "heart.fill")) { _ in
print("favorite")
}
let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up.fill")) { _ in
print("share")
}
let delete = UIAction(title: "Delete",
image: UIImage(systemName: "trash.fill"),
attributes: [.destructive]) { _ in
print("delete")
}
return UIMenu(children: [favorite, share, delete])
}
}
}
- 增加了类型为 UIAction 的属性
backAction
用于实现返回按钮事件。
代码语言:javascript
复制// Created by YungFan
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 设置backAction
navigationItem.backAction = UIAction(handler: { _ in
print("返回按钮点击实践")
})
}
}