0

I am trying to implement an UITabBarController with 2 UITabBarItems . I added in storyboard the TabBarController. I almost did it, but still I am blocked with 2 important issues:

1) Here is how tab bar should look: enter image description here

Please ignore orange button, that is not a tabItem. So I put 2 tabItems , and I want to keep white images for both tabs even if one of them is selected. I checked a lot of times with tintColor, barTintColor and no success.

Also I tried to set tabBarItem in ViewController:

override func awakeFromNib() {
    super.awakeFromNib()

    let imgHome         = UIImage(named: "btnHome")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    let imgProfile      = UIImage(named: "btnProfile")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    let imgSelectedTab  = UIImage(named: "selectedTab_imgBackground")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

    tabBarItem = UITabBarItem(title: nil, image: imgProfile, selectedImage: imgSelectedTab)
}

but no success. Any thoughts at this issue ?

2) Second issue is about selectedImage property of UITabBarItem class. The width of image does not fit the tab. I changed between devices, and for every device the selected image is over the other tab, or does not fit the current tab.(I found a solution: to have the same image but with different width for every device. But for sure this is not a good solution)

second tab selected first tab selected

Any kind of help will be fine! Many thanks

Bonnke
  • 896
  • 1
  • 12
  • 24

2 Answers2

0

You need to change you rendering mode to UIImageRenderingModeAlwaysOriginal instead of automatic.

Mika
  • 5,807
  • 6
  • 38
  • 83
  • Sorry man, I copied the text, after I played a while with renderingMode properties... I used AlwaysOriginal, and the same thing is happening. – Bonnke May 26 '15 at 15:26
0

Here is a full example of how I managed both issues: https://github.com/AndreiBoariu/TabBarController

For first issue, I solved using this for loop in UITabBarController class:

for item in tabBar.items as! [UITabBarItem] {
        if let image = item.image {
            item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal)
        }
    }

and here is the extension of UIImage

public extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)

    let context = UIGraphicsGetCurrentContext() as CGContextRef
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal)

    let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
    CGContextClipToMask(context, rect, self.CGImage)
    tintColor.setFill()
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
}
}

For second issue, check code from github ;)

Bonnke
  • 896
  • 1
  • 12
  • 24
  • Please make sure your answers here are complete and can stand on their own. A link to an external repository can break, for example, and requires that people read the whole repository. Provide a summary, at the very least. – Martijn Pieters Jun 09 '15 at 16:54
  • Thanks @MartijnPieters I will update my answer. It was pretty easy to share external link to my repo, instead of writing code, because I have to share too more details... But I commented clearly directly on app. Thanks again. – Bonnke Jun 09 '15 at 20:04
  • The external link should *support* your answer, not *be* your answer. If a question can only be answered with more code than can be reasonably posted in an answer, it should be closed as too broad instead. – Martijn Pieters Jun 09 '15 at 20:13
  • So you suggest to post details on github, right ? So user will see details there or here, and will not be forced to download the full code. Just want to be sure that I understand corectly your point. Thanks – Bonnke Jun 09 '15 at 20:24