2015-12-09 199 views
3

我在我的應用程序中創建了UITabBarController。圖像縮放爲UITabBar背景圖像

然後在viewDidLoad()我想更改UITabBar背景圖片。下面是我試圖使它的工作代碼:

class MainTabBarController: UITabBarController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     UITabBar.appearance().translucent = false 
     UITabBar.appearance().backgroundColor = UIColor.clearColor() 
     UITabBar.appearance().backgroundImage = UIImage(named: "tabbar_background") 
     UITabBar.appearance().contentMode = .ScaleAspectFit 
    } 
} 

但結果是不正確的(image)。有人可以幫我填充整個標籤框嗎?

回答

2

嘗試將image調整爲標籤欄大小。或者將imageView添加到tabBar中作爲subview,然後使用該圖中的imageView

子類的TabBarController,並添加imageview有:

var bgView: UIImageView = UIImageView(image: UIImage(named: "tabBarBackground.png")) 
bgView.frame = CGRectMake(0, 420, 320, 60)//you might need to modify this frame to your tabbar frame 
self.view.addSubview(bgView) 
+2

時發生的圖像以TabBar,所以它顯示了上述的TabBar按鈕並不能看到任何按鍵這項工作。 –

0

下面是與自定義圖標自定義半透明的標籤欄圖像代碼。您將需要通過變量直接引用標籤欄。

斯威夫特3

private func setupTabBar() { 
    print ("Setting up the Tab Bar and Items") 

    tabBarView.clipsToBounds = true 
    tabBarView.backgroundImage = UIImage() 

    let bgView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "TabBarBackground")) 
    bgView.frame = tabBarView.bounds 
    tabBarView.addSubview(bgView) 

    tabBarView.tintColor = UIColor.white 
    UITabBar.appearance().tintColor = UIColor.gray 
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal) 
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.gray], for: .selected) 

    tabBarLimitsItemView.image  = #imageLiteral(resourceName: "TabIconLimits").withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
    tabBarControlsItemView.image = #imageLiteral(resourceName: "TabIconControls").withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
    tabBarPayRulesItemView.image = #imageLiteral(resourceName: "TabIconPayRules").withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
    tabBarSettingsItemView.image = #imageLiteral(resourceName: "TabIconSettings").withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
} 
0

這對我的作品在迅速3

class MyTabBarController: UITabBarController, UINavigationControllerDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let backgroundImage = UIImageView(image: UIImage(named: "gray background")) 
    backgroundImage.frame = backgroundImage.bounds 
    self.view.addSubview(backgroundImage) 


} 
0

我解決了使用clipsToBounds這個問題。
這裏是我的例子:

let tabBar = self.tabBar 
tabBar.backgroundImage = UIImage() 
tabBar.clipsToBounds = true 

完美的iPhone X