对于所有搜索如何为 LargeTitle 设置字幕的人:
步骤1
将导航控制器添加到您的场景中:
打开你的故事板(Main.storyboard)。
选择场景。
选择编辑器 > 嵌入 > 导航控制器。
第2步
打开大标题:
选择项目场景 > 项目 > 导航栏。
在属性检查器上勾选“首选大标题”。
第 3 步
将此函数添加到您的代码中:
func setTitle(title:String, subtitle:String) -> UIView {
//Get navigation Bar Height and Width
let navigationBarHeight = Int(self.navigationController!.navigationBar.frame.height)
let navigationBarWidth = Int(self.navigationController!.navigationBar.frame.width)
//Y position for Title and Subtitle
var y_Title = 0.0
var y_SubTitle = 0.0
//Set Y position
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
//If screen height equal iPhone 5S and SE
case 1136:
y_Title = 46
y_SubTitle = 36
print("iPhone 5S and SE")
//If screen height equal iPhone 6, 6+, 6S, 6S+, 7, 7+, 8, 8+ and X
case 1334, 1920, 2208, 2436:
y_Title = 48
y_SubTitle = 38
print("iPhone 6, 6+, 6S, 6S+, 7, 7+, 8, 8+ and X")
default:
y_Title = 46
y_SubTitle = 36
print("Default")
}
}
//Set Font size and weight for Title and Subtitle
let titleFont = UIFont.systemFont(ofSize: 33, weight: UIFont.Weight.bold)
let subTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.semibold)
//Title label
let titleLabel = UILabel(frame: CGRect(x: 8.5, y: y_Title, width: 0, height: 0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.black
titleLabel.font = titleFont
titleLabel.text = title
titleLabel.sizeToFit()
//SubTitle label
let subtitleLabel = UILabel(frame: CGRect(x: 8.5, y: y_SubTitle, width: 0, height: 0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.gray
subtitleLabel.font = subTitleFont
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
//Add Title and Subtitle to View
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: navigationBarWidth, height: navigationBarHeight))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
return titleView
}
然后在中调用此函数viewDidLoad()
:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.titleView = setTitle(title: "Title", subtitle: "SUBTITLE")
}