你可以玩一下
UILabel
内部
UINavigationBarLargeTitleView
与更改其插入内容和减小字体大小一样,下面是一个示例:
// Create label
labelSubtitle.text = "Small Title"
labelSubtitle.backgroundColor = UIColor.clear
labelSubtitle.textColor = UIColor.searchBarTextColor(dark: theme)
labelSubtitle.font = UIFont.systemFont(ofSize: 14)
// Add label to subview hierarchy
for subview in self.navigationController!.navigationBar.subviews {
let stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("UINavigationBarLargeTitleView") {
let largeSubViews = subview.subviews
for sbv in largeSubViews
{
if let lbl = sbv as? UILabel
{
lbl.padding = UIEdgeInsets(top: 4, left: 0, bottom: 0, right: 0)
lbl.setNeedsLayout()
}
}
subview.addSubview(labelSubtitle)
}
}
self.navigationController!.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30, weight: .bold)]
labelSubtitle.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
labelSubtitle.leftAnchor.constraint(equalTo: self.navigationController!.navigationBar.leftAnchor, constant: 20.0),
labelSubtitle.bottomAnchor.constraint(equalTo: self.navigationController!.navigationBar.bottomAnchor, constant: -37.0),
labelSubtitle.heightAnchor.constraint(equalToConstant: 20.0),
labelSubtitle.widthAnchor.constraint(equalToConstant: 200.0)
])
要更改插图,我将使用本文中发布的扩展插件:
Adding space/padding to a UILabel
结果如下所示: