iOS11/Xcode 9 UIBarButtonItem 问题

标签 ios uibarbuttonitem ios11 xcode9 clickable

在我从 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/

相关文章:

ios - Retina iOS 设备不显示@2X 图像,它显示 1X 图像

swift - 如何通过prepareForSegue更改按钮的色调颜色

ios - UIImageView 无法在自调整单元格 iOS 8 中正确调整大小

ios - UIcollectionView Reloaddata 在显示实际数据之前显示其下方行中的一些随机数据几分之一秒

css - 使用媒体查询从纵向切换到横向时出现问题

ios - UIBarButtonItems 以错误的顺序出现

ios - 在带有 cocoa pod 的 Xcode 中使用 swift 安装 firebase 时,它​​给了我 27 个错误

swift - iOS/swift : QLPreviewController shows blank page in iOS11

xamarin.ios - IOS 11 显示问号 (?) 字符而不是单引号 (') or Double Quote ("")

ios - 交互式 Pop Gesture 的安全区域底部布局问题