0

So I found out about a way to keep tabbar icon colors by putting

for item in (self.tabBarController?.tabBar.items as NSArray!){
        (item as! UITabBarItem).image = (item as! UITabBarItem).image?.imageWithRenderingMode(.AlwaysOriginal)
    }

That simple block of code in your viewdidload on any viewcontroller file.

This is a fantastic solution but I'm having problems as it crashes solely due to a page returning a nil value.

I'm at my wits end trying to figure out why its returning nil and crashing when going to another view controller. It's quite annoying.

Is there an alternative way to keep my original tabbar colors unselected, utilizing swift?

I have obviously included an image of this working quite well albeit my crash. Note the lack of default grey icons.

enter image description here

enter image description here

Attempted solutions.

Move code to viewDidAppear - Failed UIImagerendering - Failed

James Chen
  • 387
  • 1
  • 4
  • 17
  • I believe this is the best solution. I think you should try to fix the crash. Could you please post the crash logs? You mentioned it crashes due to a page returning a nil value. What does that mean? – Abhinav Sep 27 '15 at 05:42
  • @Abhinav I currently have one view controller producing two different tables. When I press a button the items change and the new data is presented on the same view controller. The crash occurs here. The 'second' view controller (same one mind you, sorry if this is confusing) has no tabbar, so I'm under the impression that this has something to do with it. – James Chen Sep 27 '15 at 05:45
  • I get it. So, you are using the same VC to render data in two different ways - this makes sense. But what do you mean that on tap on button in second mode of VC, there is no tabbar? Should your table view only not change and tab bar remains in there? How are you then switching back to first table? – Abhinav Sep 27 '15 at 05:51
  • @Abhinav I've posted a gif of the situation for clarity. The 'your posts' page has no tabbar. – James Chen Sep 27 '15 at 05:55
  • I see. Could you please show the crashing code. – Abhinav Sep 27 '15 at 06:33
  • Why dont you create an image from resource using UIImage(named:"whatever").imageWithRenderingMode(.AlwaysOriginal)? – Leo Dabus Sep 27 '15 at 06:40
  • If none of the solutions solve your problem please let me know ao I can reopen the question – Leo Dabus Sep 27 '15 at 06:48
  • You can also try moving you code to viewDidAppear – Leo Dabus Sep 27 '15 at 06:52
  • @LeoDabus I'll try that now. – James Chen Sep 27 '15 at 06:54
  • @LeoDabus unfortunately that doesn't work quite well. I'd like the question to remain open for at least another day. – James Chen Sep 27 '15 at 06:56
  • Can you edit your question and add what didn't work "quite well" ? All you need is an UIImage wiith custom rendering. Did yiu try your code inside viewDidAppear? – Leo Dabus Sep 27 '15 at 07:02
  • @LeoDabus I did try that code in viewdidappear, and it still crashed due to it being nil when wrapping an optional value. I'm not getting much more than that from the crash logs. – James Chen Sep 27 '15 at 07:21
  • check my answer here: https://stackoverflow.com/questions/22846652/preserving-the-original-image-color-of-the-selected-and-unselected-uitabbar-icon/44186080#44186080 – Abdoelrhman May 25 '17 at 17:09

0 Answers0