【问题标题】:iOS11/Xcode 9 UIBarButtonItem IssueiOS11/Xcode 9 UIBarButtonItem 问题
【发布时间】:2017-10-16 17:41:33
【问题描述】:

在我从 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];

【问题讨论】:

    标签: ios uibarbuttonitem ios11 xcode9 clickable


    【解决方案1】:

    已解决..原来是a UIToolbar issue

    这是 iOS 11 上的一个已知错误。UIToolbar 子视图无法获取触摸事件,因为工具栏的某些内部视图设置不正确。

    当前的解决方法是在添加子视图之前调用toolBar.layoutIfNeeded()

    在你的情况下:

    inputFieldView.layoutIfNeeded()
    

    希望这将在下一个主要版本中得到解决。

    【讨论】: