简体   繁体   中英

iOS11/Xcode 9 UIBarButtonItem Issue

UIBarbutton item does not respond to clicks after I built my app from Xcode 9. Its working on Xcode 8.

Apparently found that there are issues as described in this post below.. ( UIBarButtonItem not clickable on iOS 11 beta 7? )

The problem is I added the constraints as explained in the post and still wont work..Can someone look at my code and tell me what I am doing wrong?

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];

Resolved.. turns out it was a UIToolbar issue .

This is a known bug on iOS 11. UIToolbar subviews don't get the touch events because some internal views to the toolbar aren't properly setup.

The current workaround is to call toolBar.layoutIfNeeded() right before adding subviews.

In your case:

inputFieldView.layoutIfNeeded()

Hopefully this will get fixed on the next major version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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