2017-10-16 130 views
0

UIBarbutton項目不響應點擊我建我的應用程序後,在Xcode 9.及其對Xcode的8iOS11/Xcode的9的UIBarButtonItem問題

工作顯然發現有如下這篇文章所描述的問題。( 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]; 

回答

0

已解決..原來是a UIToolbar issue

這是iOS 11上的一個已知錯誤。UIToolbar子視圖不會獲取觸摸事件,因爲工具欄的某些內部視圖未正確設置。

當前的解決方法是在添加子視圖之前立即調用toolBar.layoutIfNeeded()

你的情況:

inputFieldView.layoutIfNeeded() 

希望這將得到固定的下一個主要版本。