我想在UINavigationBar的大标题视图上添加一个自定义子视图,就像App Store在iOS 11中所做的那样。(右侧的"用户图标")
我们可以通过UINavigationItem.titleView访问传统的导航栏区域,但似乎没有API可以访问大型标题视图区域。
https://developer.apple.com/documentation/uikit/uinavigationitem/https://developer.apple.com/documentation/uikit/uinavigationbar/
我使用视图层次结构调试器确认了名称为"_UINavigationBarLargeTitleView"。我可以在上面添加自定义视图吗?
解决方案依赖于大标题标签的子视图顺序,而不是其私有类名,以使其符合AppStore指南。
class CustomLargeTitleNavigationBar: UINavigationBar {
override func didMoveToSuperview() {
super.didMoveToSuperview()
if #available(iOS 11.0, *) {
for subview in subviews {
if let largeTitleLabel = subview.subviews.first(where: { $0 is UILabel }) as? UILabel {
let largeTitleView = subview
print("largeTitleView:", largeTitleView)
print("largeTitleLabel:", largeTitleLabel)
// you may customize the largeTitleView and largeTitleLabel here
break
}
}
}
}
}
基于本文,我想它应该或多或少是这样的。https://qiita.com/KikurageChan/items/4152c2d8ac89c601a054
import UIKit
@IBDesignable class CustomNavigationBar: UINavigationBar {
override func layoutSubviews() {
super.layoutSubviews()
if #available(iOS 11.0, *) {
for subview in self.subviews {
let stringFromClass = NSStringFromClass(subview.classForCoder)
if stringFromClass.contains("UINavigationBarLargeTitleView") {
//subview.frame.size.height = 30
// Custom view
subview.addSub.....
}
}
}
}
}
自定义导航栏