我知道可以使用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
}
}
}
此解决方案有效,但是当您从“大”标题切换为“小”标题并向后抽动时,它看起来很不方便
答案 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)!]
答案 1 :(得分:0)
您可以尝试这样:
navigationController?.navigationBar.prefersLargeTitles = true
let NavigationTitle = "Sign in"
navigationController?.navigationBar.topItem?.title = NavigationTitle.uppercased()
答案 2 :(得分:0)