我正在尝试实现这个 image of my implementation .
uilabel 下面的三个按钮是可以点击的。我有一个 UIView 作为我的自定义导航栏 View 的 subview ,然后该 View 中有两个 View ,一个是 uilabel,第二个是 uibuttons 的 uiview i
我已经尝试从其他答案中实现解决方案,例如
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.point(inside: point, with: event) {
self.isUserInteractionEnabled = true
}else{
self.isUserInteractionEnabled = false
}
return super.hitTest(point, with: event)
}
但这没有用。我注意到,如果我在默认 uinavigation 栏大小位置附近的按钮上方点击,则可以识别点击。
如果有帮助,我的导航栏的大小是 CGSize(width: self.frame.size.width, height: 100)
。
更新
只需添加我的自定义导航类及其用法
class CustomNavigationBar: UINavigationBar {
override func sizeThatFits(_ size: CGSize) -> CGSize {
let newSize :CGSize = CGSize(width: self.frame.size.width, height: 100)
return newSize
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.point(inside: point, with: event) {
self.isUserInteractionEnabled = true
}else{
self.isUserInteractionEnabled = false
}
return super.hitTest(point, with: event)
}
let navigationController = UINavigationController(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
navigationController.setViewControllers([mainController], animated: false)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
最佳答案
请检查您的导航栏高度是否增加,这将是停止导航栏完全交互的原因,添加以下代码并检查将正常工作,
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let height: CGFloat = 100 //whatever height you want
let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height + height)
}
关于ios - 自定义导航栏全 View 不接收触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45153971/