2015-02-11 66 views
0

我試圖查看我的TabBar是否爲零。檢查tabBar是否爲

在Objective-C,我會做這樣:

if(self.tabBar != nil){ 
} 

如果我嘗試這樣做是爲了我迅速返回此錯誤:

'UITabBar' 不是的NSString的「亞型「

這是我在整個迅速編寫代碼:

override func viewDidLayoutSubviews() { 
    //check tabBar not null 
    if (self.tabBar != nil) 
    { 
     //make changes in frame here according to orientation if any 
     self.tabBar.frame = CGRect(x: 00, y: 20, width:self.view.bounds.size.width, height: 49) 
    } 
} 
+0

嗨,嘗試'如果(self.tabBar){ }' – Saif 2015-02-11 10:04:23

+0

讓我的錯誤: 'UITabBar' 不符合protocoll布爾類型 – Stephany 2015-02-11 10:11:03

回答

0

嘗試,

let tabBar: UITabBar? 
tabBar = //initialize tabBar 
if tabBar != nil { 

} 

希望這有助於

2

的的viewController的tabBarController是可選的。 UITabBarController中的tabBar不是可選的。因此,你可以嘗試:

override func viewDidLayoutSubviews() { 
     if let tabBarController = self.tabBarController { 
      // use your tabBarController 
      tabBarController.tabBar // the tabBar in the tabBarController is not an optional 
     } 
    } 
+0

我假設你要重寫一個標準'ViewController'如果你重寫了'UITabBarController',那麼你的'tabBar'不是可選的,所以你不需要無核查。 – 2015-02-11 10:31:08