7

我在應用程序中實現了一個主題,並遇到了一個奇怪的錯誤(功能?)。出於某種奇怪的原因,我不能在我的自定義UITabBarController類中使用UIView.animate來動畫我的UITabBar的顏色變化,但相同的確切代碼在我的UINavigationController的自定義類中完美地工作。它是如何使UINavigationBar的條形顏色變化而不是UITabBar動畫的?

我錯過了什麼嗎?有什麼我可以用來動畫顏色變化?我瀏覽過蘋果文檔,但什麼都沒發現。

下面是我用在這兩種情況下的代碼:

class customNavigationController: UINavigationController { 
    @IBOutlet weak var navBar = ThemeManager.navigationbar 

    func dusk(notification: NSNotification) { 
     UIView.animateWithDuration(1, animations: { 
      self.navBar?.barTintColor = UIColor(red: 79/255, green: 79/255, blue: 79/255, alpha: 1) 
      self.navBar?.barStyle = UIBarStyle.Black 

     }) 
    } 
} 

和:

class customTabController: UITabBarController { 
    @IBOutlet weak var tab = ThemeManager.tabbar 

    func dusk(notification: NSNotification) { 
     UIView.animateWithDuration(1, animations: { 
      self.tab?.barTintColor = UIColor(red: 79/255, green: 79/255, blue: 79/255, alpha: 1) 
      self.tab?.barStyle = UIBarStyle.Black 
     }) 
    } 
} 

回答

9

可以使用UIView.transitionWithView在兩種狀態之間褪色

UIView.transitionWithView(self.tab!, duration: 1.0, options: .BeginFromCurrentState | .TransitionCrossDissolve, animations: {() -> Void in 
     self.tab!.barTintColor = UIColor(red: 79/255, green: 79/255, blue: 79/255, alpha: 1) 
     self.tab!.barStyle = UIBarStyle.Black 
    }, completion: nil) 
+0

這對動畫沒有任何影響:( – cyril

+0

這很奇怪,在我發佈之前我自己試過了,你能分享一下你的代碼嗎? –

+0

這和上面第二個引用的代碼是一樣的。我替換了'UIView.animateWithDuration(1,動畫:{self.tab?.barTintColor = UIColor(red:79/255,green:79/255,blue:79/255,alpha:1) self.tab ?. barStyle = UIBarStyle.Black })' – cyril

0

那麼要親自改變標籤欄上的顏色我使用不同顏色的完全不同的圖像enter image description here

正如你可以看到我選擇的圖像是我的第二張圖片。現在,它不會顯示圖像,除非我把它添加到我的「用戶自定義屬性運行」

enter image description here

設置的keyPath爲「selectedImage」,類型爲「圖像」和價值,你的形象,您想在展示時顯示。我希望這回答了你的問題。