【问题标题】:Customized navigation bar does not receive touch events in entire view自定义导航栏在整个视图中不接收触摸事件
【发布时间】:2017-07-17 21:30:20
【问题描述】:

我正在尝试实现这个image of my implementation

uilabel 下方的三个按钮是可点击的。我有一个 UIView 作为我的自定义导航栏视图的子视图,然后在该视图中有两个视图,一个是 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)
}

但这没有用。我注意到,如果我点击默认导航栏大小位置附近的按钮上方,那么点击就会被识别。

如果有帮助的话,我的导航栏的大小是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()

【问题讨论】:

    标签: ios swift uinavigationbar


    【解决方案1】:

    请检查您的导航栏高度是否增加,这将是停止导航栏完全交互的原因,添加以下代码并检查将正常工作,

    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)
    
    }
    

    【讨论】:

    • 嘿 Manikanta,我已经尝试过你的建议,但没有奏效。
    • 能否分享一下你自定义导航栏的代码。
    • 我已经用我的自定义类以及我如何使用它来更新我的问题。
    • 在你的mainController的viewDidAppear中,请写上上面的代码并检查一次。
    【解决方案2】:

    您好,您应该只在 viewdidappear 中更改导航栏框架,否则框架将不起作用。

    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: 0, width:width, height:  height)
    

    【讨论】:

      【解决方案3】:

      找到问题了!

      包含标题和按钮的视图设置为高度为 60 的框架,而不是我的自定义导航栏的高度。谢谢大家的帮助。

      这里的教训是

      在为导航栏设置自定义高度后,确保它的 subView 的边界与导航栏的边界匹配。

      【讨论】:

      • 嘿,你能发布一些解决方案的代码吗?我遇到了同样的问题,我尝试自定义导航栏,但不知何故无法触发对自定义视图内任何按钮的点击
      • 嗨 Shivam,我无法再访问代码,但如果您遇到的问题与我遇到的问题完全相同,那么您应该将视图的 clipsToBounds 属性设置为 true 以帮助调试。
      • 嗨伍迪,感谢您的建议。我能够解决这个问题,它与自动布局约束有关。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      相关资源
      最近更新 更多