在我从 Xcode 9 构建我的应用程序后,UIBarbutton 项目不响应点击。它在 Xcode 8 上工作。
显然发现存在下面这篇文章中描述的问题.. ( UIBarButtonItem not clickable on iOS 11 beta 7? )
问题是我按照帖子中的说明添加了约束,但仍然无法正常工作。有人可以看看我的代码并告诉我我做错了什么吗?
UIImage* image = [UIImage imageNamed:@"test.png"];
CGRect frame = CGRectMake(0, 0, 30, 30);
UIButton* someButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[someButton setImage:image forState:UIControlStateNormal];
[someButton setFrame:frame];
[someButton setShowsTouchWhenHighlighted:YES];
[someButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
NSLayoutConstraint * widthConstraint = [someButton.widthAnchor constraintEqualToConstant:30];
NSLayoutConstraint * HeightConstraint =[someButton.heightAnchor constraintEqualToConstant:30];
[widthConstraint setActive:YES];
[HeightConstraint setActive:YES];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:someButton];
self.toolbar.items = [NSArray arrayWithObjects:item, nil];
最佳答案
已解决..原来是a UIToolbar issue .
这是 iOS 11 上的一个已知错误。UIToolbar subview 无法获取触摸事件,因为工具栏的某些内部 View 未正确设置。
当前的解决方法是在添加 subview 之前调用 toolBar.layoutIfNeeded()
。
在你的情况下:
inputFieldView.layoutIfNeeded()
希望这会在下一个主要版本中得到修复。
关于iOS11/Xcode 9 UIBarButtonItem 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46776076/