1

長話短說iOS10左側,右側按鈕,自定義標籤沒有問題,iOS11沒有顯示。我在其他地方看過,我需要爲按鈕添加約束條件,但這沒有幫助。在viewDidLoad()中調用的代碼。UIBarButtonItem不能在ios11中工作

self.connectionButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0,0.0,74.0,29.0)]; 
[self.connectionButton.widthAnchor constraintEqualToConstant:74].active = YES; 
[self.connectionButton.heightAnchor constraintEqualToConstant:29].active = YES; 
self.connectionButton.backgroundColor = [UIColor yellowColor]; 
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:self.connectionButton]; 
self.navigationItem.rightBarButtonItem = buttonItem; 

外觀:

[[UINavigationBar appearance] setTranslucent:YES]; 
[[UINavigationBar appearance] setShadowImage:[UIImage new]]; 
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]]; 

當我檢查在運行時框架是正確的(0,0,74,29)。雖然沒有按鈕顯示在欄中。

XCode 9 beta 8,不能在任何設備或模擬器上工作。

+0

請分享您的完整NavBar設置(外觀配置等)。你在哪種方法設置rightBarButtonItem? –

+0

已編輯。 Afaik沒什麼特別的,默認在故事板上用於根控制器。 – Tom

+0

我也有同樣的問題,在ios11 barbuton時間iboutlet不工作...它顯示不是密鑰值編碼兼容的關鍵,任何幫助 – Tech

回答

0

隨着更多的測試,我發現導航欄不顯示任何東西 - 甚至沒有默認外觀的標題。後浪費在評論小時/無關的代碼取消註釋塊,我發現罪魁禍首:

override var traitCollection: UITraitCollection { 
     var horizTraitCollection = UITraitCollection(horizontalSizeClass: .regular) 
     if view.bounds.width < view.bounds.height { 
      horizTraitCollection = UITraitCollection(horizontalSizeClass: .compact) 
     } 

     return UITraitCollection(traitsFrom: [horizTraitCollection, UITraitCollection(verticalSizeClass: super.traitCollection.verticalSizeClass)]) 
    } 

這是我使用的縱向/橫向呈現覆蓋大小類。 IMO與導航欄外觀完全無關。不知道爲什麼它打破導航欄或如何解決它。

編輯︰經過一些調整後,我能夠得到它在iPad上,但不是iPhone的工作。經過一些調整,我得到它在iPhone上工作以及:

override var traitCollection: UITraitCollection { 
    if UIDevice.current.userInterfaceIdiom == .pad { 
     var horizTraitCollection = UITraitCollection(horizontalSizeClass: .regular) 
     if UIDevice.current.orientation.isPortrait { 
      horizTraitCollection = UITraitCollection(horizontalSizeClass: .compact) 
     } 
     return UITraitCollection(traitsFrom: [horizTraitCollection, UITraitCollection(verticalSizeClass: super.traitCollection.verticalSizeClass)]) 
    } 
    return super.traitCollection 
} 
相關問題