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

Max*_*Max 2 uinavigationbar swift ios11

我知道可以使用分别为«大»和«小»标题设置字体系列,字体大小和颜色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)
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从视图控件设置标题:

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
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

此解决方案有效,但是当您从“大”标题切换为“小”标题并向后抽动时,它看起来非常有问题

Raj*_*r R 5

您可以使用小写字体将大写的标题大写,小写的大写。

更改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)!]
    }
}
Run Code Online (Sandbox Code Playgroud)

或者您可以自定义字体。我在http://www.glyphrstudio.com/online/中使用OpenSans创建了一种新样式的字体

你可以在这里下载

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)!]
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明