iOS 11 未点击 navigationItem 的自定义 titleView
custom titleView of navigationItem is not getting tapped on iOS 11
我正在使用自定义 titleView 并将其分配给 navigationItem titleView。在 iOS 11 之前,它一直工作正常。自从更新后,它的位置错位到中心,因为它最初是在更左边。除此之外,用户交互不起作用。
titleView = Bundle.main.loadNibNamed("SomeNib", owner: self, options: nil)?.first as? SomeNib
navigationItem.titleView = titleView
titleView只是一个普通的笔尖。
然后启用交互:
if let titleView = self.navigationItem.titleView {
let tap = UITapGestureRecognizer(target: self, action: #selector(onTitleViewTap))
titleView.addGestureRecognizer(tap)
titleView.isUserInteractionEnabled = true
}
在 iOS 11 中,titleView 正在使用自动布局进行设置。因此,titleView 的大小是您在 titleView 中设置的视图的固有大小。
您视图中的这段代码 class(您设置为 titleView)应该对您有所帮助:
override var intrinsicContentSize: CGSize {
return UILayoutFittingExpandedSize
}
我用 xib 创建了一个自定义 UIView。在 UIView 中,我在顶部添加了 UILabel
、ImageView
和 UIButton
。单击隐藏按钮时,我可以调用单击事件。
导入 UIKit
class NavBarTitleView: UIView {
@IBOutlet weak var title : UILabel!
@IBOutlet weak var clickButton: UIButton!
/// Create an instance of the class from its .xib
class func instanceFromNib() -> NavBarTitleView {
return UINib(nibName: "NavBarTitleView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! NavBarTitleView
}
//this line of code will help to enable a click event.
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
}
然后在 UIViewcontroler 中,我添加了下面的代码。
if let title = self.navBarTitle{
if navbarTitleView == nil {
navbarTitleView = NavBarTitleView.instanceFromNib()
self.navigationItem.titleView = navbarTitleView
}
navbarTitleView!.title.text = title
navbarTitleView!.clickButton.addTarget(self, action: #selector(didTapNavbarTitle(_:)), for: .touchUpInside)
navbarTitleView!.layoutIfNeeded()
}else{
navigationItem.title = ""
}
按钮操作:
@objc func didTapNavbarTitle(_ sender: Any){
print("NavBar Selected")
}
结果 -
我希望这会奏效。在代码 11.6 和 os 版本 13.6
上测试
我正在使用自定义 titleView 并将其分配给 navigationItem titleView。在 iOS 11 之前,它一直工作正常。自从更新后,它的位置错位到中心,因为它最初是在更左边。除此之外,用户交互不起作用。
titleView = Bundle.main.loadNibNamed("SomeNib", owner: self, options: nil)?.first as? SomeNib
navigationItem.titleView = titleView
titleView只是一个普通的笔尖。
然后启用交互:
if let titleView = self.navigationItem.titleView {
let tap = UITapGestureRecognizer(target: self, action: #selector(onTitleViewTap))
titleView.addGestureRecognizer(tap)
titleView.isUserInteractionEnabled = true
}
在 iOS 11 中,titleView 正在使用自动布局进行设置。因此,titleView 的大小是您在 titleView 中设置的视图的固有大小。
您视图中的这段代码 class(您设置为 titleView)应该对您有所帮助:
override var intrinsicContentSize: CGSize {
return UILayoutFittingExpandedSize
}
我用 xib 创建了一个自定义 UIView。在 UIView 中,我在顶部添加了 UILabel
、ImageView
和 UIButton
。单击隐藏按钮时,我可以调用单击事件。
导入 UIKit
class NavBarTitleView: UIView {
@IBOutlet weak var title : UILabel!
@IBOutlet weak var clickButton: UIButton!
/// Create an instance of the class from its .xib
class func instanceFromNib() -> NavBarTitleView {
return UINib(nibName: "NavBarTitleView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! NavBarTitleView
}
//this line of code will help to enable a click event.
override var intrinsicContentSize: CGSize {
return UIView.layoutFittingExpandedSize
}
}
然后在 UIViewcontroler 中,我添加了下面的代码。
if let title = self.navBarTitle{
if navbarTitleView == nil {
navbarTitleView = NavBarTitleView.instanceFromNib()
self.navigationItem.titleView = navbarTitleView
}
navbarTitleView!.title.text = title
navbarTitleView!.clickButton.addTarget(self, action: #selector(didTapNavbarTitle(_:)), for: .touchUpInside)
navbarTitleView!.layoutIfNeeded()
}else{
navigationItem.title = ""
}
按钮操作:
@objc func didTapNavbarTitle(_ sender: Any){
print("NavBar Selected")
}
结果 -
我希望这会奏效。在代码 11.6 和 os 版本 13.6
上测试