swift - 如何使用 prefersLargeTitles 更改所有权注册表?

标签 swift uinavigationbar ios11

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

问题是:导航 Controller 是否有任何选项可以在打开的导航面板中以大写字母显示 «large title»?

enter image description here

现在我使用自定义导航 Controller :

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 Controlle 设置标题:

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

}

这个解决方案有效,但是当你从“大”标题切换到“小”标题并向后切换时,它会抽动并且看起来很糟糕

最佳答案

您可以使用 Small caps fonts 为 «large title» 设置大写标题,为 «small title» 设置大写标题

titleTextAttributes改成其他字体,将largeTitleTextAttributes改成caps字体

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

关于swift - 如何使用 prefersLargeTitles 更改所有权注册表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370707/

相关文章:

ios11 - UIDocumentBrowserViewController 为空

ios - NSLocale 在 iOS 11 中返回错误值

ios - Tinder 个人资料动画

swift - NavigationItem TitleView 约束不起作用

ios - 扩展 UINavigationBar 高度

iphone - UINavigationBar 上图像的大小

ios - 如何修复 Xcode 错误 : Could not locate device support files on iPhone with Xcode

ios - SKProduct 崩溃 - 仅在商店版本的应用程序中

swift - 如何将NSDataAsset快速转换为AVAsset。我希望能够使用它进行编辑

ios - Spotify iOS SDK 返回不存在的歌曲? - swift