Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image scaling for UITabBar background image

I've created UITabBarController in my application.

Then in viewDidLoad() I want to change UITabBar background image. Here is the code I'm trying to make it works:

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
    }
}

But the result isn't correct (image). Can someone help me to make it fill the entire tabbar frame?

like image 590
mkz Avatar asked Dec 09 '15 13:12

mkz


3 Answers

I solved this problem using clipsToBounds.
Here are my example:

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

This work perfectly on iPhone X

like image 56
John Lima Avatar answered Nov 05 '22 13:11

John Lima


Try resizing the image to the tab bar size. Or Add an imageView to the tabBar as subview, and then use the image in that imageView.

Subclass the TabBarController and add imageview there:

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)
like image 20
Teja Nandamuri Avatar answered Nov 05 '22 13:11

Teja Nandamuri


This works for me in swift 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)


}
like image 44
QQ10 Avatar answered Nov 05 '22 12:11

QQ10