1. 添加了类型为 UINavigationItem.ItemStyle 的特点style用于描绘 UINavigationItem 在 UINavigationBar 上的布局,共有 3 种样式:navigator(默认样式),browser(浏览器样式)与editor(编辑器样式)。
//  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)
    }
}
  1. 添加了闭包类型的特点titleMenuProvider用于给当时导航栏的标题添加操作菜单。
//  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])
        }
    }
}
  1. 添加了类型为 UIAction 的特点backAction用于实现回来按钮的点击事情。
//  Created by YungFan
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置backAction
        navigationItem.backAction = UIAction(handler: { _ in
            print("回来按钮点击事情")
        })
    }
}