Swift 如何使用PreferSlargetTitles更改标题注册表?

Swift 如何使用PreferSlargetTitles更改标题注册表?,swift,uinavigationbar,ios11,Swift,Uinavigationbar,Ios11,我知道可以使用prefersLargeTitles分别设置«大»和«小»标题的字体系列、字体大小和颜色 问题是:导航控制器是否有任何选项可以在打开的导航面板中以大写字母显示“大标题” 现在我使用自定义导航控制器: class MyNavigationController: UINavigationController { public var titleSaved: String? override func viewDidLayoutSubviews() {

我知道可以使用
prefersLargeTitles
分别设置«大»和«小»标题的字体系列、字体大小和颜色

问题是:导航控制器是否有任何选项可以在打开的导航面板中以大写字母显示“大标题”

现在我使用自定义导航控制器:

class MyNavigationController: UINavigationController {

    public var titleSaved: String?

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        guard let topItem = navigationBar.topItem else {
            return
        }

        if navigationBar.frame.size.height > 60 {
            topItem.title = topItem.title?.uppercased()
        } else {
            if let titleSaved = titleSaved {
                topItem.title = titleSaved
            } else {
                topItem.title = topItem.title?.applyingTransform(StringTransform(rawValue: "Title"), reverse: false)
            }
        }
    }
}
从视图控件设置标题:

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.prefersLargeTitles = true

        let title = "Sign In"
        navigationItem.title = title

        if let nc = navigationController as? MyNavigationController {
            nc.titleSaved = title
        }
    }

}
此解决方案可行,但当您从“大”标题切换到“小”标题并向后切换时,它会抖动,并且看起来非常有问题,您可以这样尝试:

navigationController?.navigationBar.prefersLargeTitles = true 
let NavigationTitle = "Sign in"
navigationController?.navigationBar.topItem?.title = NavigationTitle.uppercased()

您只需将导航栏标题设置为大写即可。并将PregferSlargetiles设置为true

self.navigationController?.navigationBar.prefersLargeTitles = true
self.title = "title".uppercased()
选中此项:


您可以使用大写的“大标题”标题和大写的“小标题”

用其他字体更改
titleTextAttributes
,用大写字体更改
largeTitleTextAttributes

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Sign In"
        self.navigationController?.navigationBar.prefersLargeTitles = true
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
        self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.red,
                                                                             .font:UIFont(name: "Panton-LightCaps", size: 30)!]
    }
}

也可以自定义字体。我在中使用OpenSAN创建了一种新样式的字体

你可以下载它


您还想增加导航栏的高度吗?请查看这个简单的。
self.title = "Sign In"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.titleTextAttributes = [.font:UIFont(name: "OpenSans-Regular", size: 30)!]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.font:UIFont(name: "MyFontRegular", size: 30)!]