简体   繁体   English

不会调用iOS11 UIBarButtonItem操作

[英]iOS11 UIBarButtonItem action not get called

I used Xcode9 Beta6 to build the project, the action was called correctly on iOS10 device, however it is not work on iOS11 device. 我使用Xcode9 Beta6来构建项目,在iOS10设备上正确调用了该操作,但它在iOS11设备上无法正常工作。

In My project, there are some viewControllers have a UIToolBar on the top, and the toolBar contains some UIBarButtonItems. 在我的项目中,有一些viewControllers顶部有一个UIToolBar,而toolBar包含一些UIBarButtonItems。

There is one this kind of viewController, whose UIBarButtonItem action is not called when I tap the UIBarButtonItem. 有一种这样的viewController,当我点击UIBarButtonItem时,不会调用其UIBarButtonItem动作。 I can see the tapping animation (the icon become dim first and back to normal after finger released) 我可以看到点击动画(图标变得暗淡,手指释放后恢复正常)

At the end of viewDidLoad , I print the info of toolbar.items to indicate that target action are set properly. 在结束viewDidLoad ,我打印的信息toolbar.items表明,目标行为是否设置正确。

Debug Output 调试输出

I solved this problem by removing a current gesture recognizer from my view and adding a new one. 我通过从视图中删除当前手势识别器并添加新手势识别器来解决此问题。 Than I opened the connections inspector of my view and add gestureRecognizer connection to my gesture recognizer. 比我打开视图的连接检查器并将gestureRecognizer连接添加到我的手势识别器。

Apple has confirmed this bug. Apple已确认此错误。 My temporary solution is changing the gesture recognizer area by removing the overlap area, so that the tap gesture won't block the tap event on UIBarButtonItem. 我的临时解决方案是通过移除重叠区域来更改手势识别器区域,以便轻击手势不会阻止UIBarButtonItem上的点击事件。

In my case I was setting up the button and instantiating it as a property of the vc 在我的情况下,我正在设置按钮并将其实例化为vc的属性

class myVC: UIViewController {
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}

If I moved this to ViewDidLoad it resolved the problem 如果我把它移到ViewDidLoad它解决了问题

class myVC: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
    let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}

} }

It is happening only for iOS 11 and when custom UIView used for rightBarButtonItem (or left also). 它仅适用于iOS 11以及用于rightBarButtonItem (或左侧)的自定义UIView If you are using UIBarButtonItem then it will work fine. 如果您正在使用UIBarButtonItem那么它将正常工作。 There is 0 width of this custom bar item, so we need to set it to something. 此自定义条形项的宽度为0,因此我们需要将其设置为某个值。

In viewDidLoad of this controller you can add code, you can replace 100 to something what will work for you: 在此控制器的viewDidLoad ,您可以添加代码,您可以将100替换为适合您的内容:

if let view = self.navigationItem.rightBarButtonItem?.customView {
    view.widthAnchor.constraint(equalToConstant: 100).isActive = true
}

At least as an easy temporary solution it is fine. 至少作为一个简单的临时解决方案,它很好。

In my case the problem was a gestureRecognizer added to the whole main view (in order to close the keyboard) like this: 在我的情况下问题是一个gestureRecognizer添加到整个主视图(为了关闭键盘),如下所示:

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard)];
[self.view addGestureRecognizer:gesture];

That gesture recognizer overrides the tap on the UIBarButtonItem, thus I solved by creating a new subview placed immediately below the navigation bar and assigning the gesture recognizer to that view and not to the whole main view. 该手势识别器会覆盖UIBarButtonItem上的点击,因此我通过创建位于导航栏正下方的新子视图并将手势识别器分配给该视图而不是整个主视图来解决。

使用@objc标记您要定位的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM