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

问题描述 投票:2回答:3

我知道可以使用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)
            }
        }
    }
}

从View Controller设置标题:

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

}

这个解决方案有效,但是当你从“大”标题切换到“小”标题并向后转,它会抽搐,看起来很漂亮

swift uinavigationbar ios11
3个回答
5
投票

你可以使用Small caps fonts获得«大标题»的大写标题和«小标题»的大写标题

用其他字体更改titleTextAttributes并用caps字体更改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


0
投票

你可以尝试这样:

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

0
投票

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

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

检查一下:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.