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

时间:2019-03-27 06:00:23

标签: swift uinavigationbar ios11

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

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

enter image description here

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

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

}

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

3 个答案:

答案 0 :(得分:5)

您可以使用Small caps fonts

将大写标题«大标题»和大写标题«小标题»

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)!]
    }
}

或者您可以自定义字体。我在http://www.glyphrstudio.com/online/

中使用OpenSans创建了一种新样式的字体

您可以here下载它

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)!]

enter image description here

答案 1 :(得分:0)

您可以尝试这样:

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

答案 2 :(得分:0)

您只是尝试将导航栏标题设置为大写。并将pregfersLargeTitles设置为true

self.navigationController?.navigationBar.prefersLargeTitles = true
self.title = "title".uppercased()

检查:

enter image description here