如何使用 prefersLargeTitles 更改标题注册表?
Posted
技术标签:
【中文标题】如何使用 prefersLargeTitles 更改标题注册表?【英文标题】:How to change the title registry using prefersLargeTitles? 【发布时间】:2019-03-27 06:00:22 【问题描述】:我知道可以使用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
此解决方案有效,但是当您从“大”标题切换到“小”标题并向后切换时,它会抽搐并且看起来很麻烦
【问题讨论】:
你也想增加导航栏的高度吗? 看看这个简单的solution。 【参考方案1】:您可以使用Small caps fonts为«large title»设置大写标题,为«small title»设置大写标题
把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)!]
【讨论】:
【参考方案2】:你可以这样试试:
navigationController?.navigationBar.prefersLargeTitles = true
let NavigationTitle = "Sign in"
navigationController?.navigationBar.topItem?.title = NavigationTitle.uppercased()
【讨论】:
【参考方案3】:您只是尝试将导航栏标题设置为大写。并将 pregfersLargeTitles 设置为 true
self.navigationController?.navigationBar.prefersLargeTitles = true
self.title = "title".uppercased()
检查一下:
【讨论】:
以上是关于如何使用 prefersLargeTitles 更改标题注册表?的主要内容,如果未能解决你的问题,请参考以下文章
iOS 11 prefersLargeTitles 以 uibutton 作为标题
UINavigationBar.prefersLargeTitles 打开时的错误 UIRefreshControl 行为
更改导航栏“prefersLargeTitles”时的平滑过渡
iOS 11 的 Refresh Controller 和 navigationBar 问题将 prefersLargeTitles 设置为 true