2015-12-09 293 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