0

I have an error when I use StoryBoard Reference after a UITabBarController (which is a custom one).

I found how to customise my icon image, but in my code, when I try to get this image, the value doesn't exist.

Here is my image configuration:

Image configuration

And there the error when I run my app:

Error

It worked before I used a Storyboard Reference. And the Image is set as my other Tab Bar Items, so I don't understand why I get this error when I run my app.


EDIT: Where do I call the setup() function?

I have 3 files to handle my Tab Bar.

The first one, CustomTabBarController.swift, contains this code in viewDidLoad:

let customTabBar = CustomTabBar(frame: self.tabBar.frame)
customTabBar.datasource = self
customTabBar.delegate = self
customTabBar.setup()

It calls the setup() method of CustomTabBar, which is:

func setup() {
    // get tab bar items from default tab bar
    tabBarItems = datasource.tabBarItemsInCustomTabBar(self)

    customTabBarItems = []
    tabBarButtons = []

    selectedTabBarItemIndex = 0

    let containers = createTabBarItemContainers()
    createTabBarItems(containers)

    customTabBarItems[selectedTabBarItemIndex].iconView.tintColor = UIColor(red:0.44, green:0.52, blue:1.00, alpha:1.0)
}

This function calls the setup() function of my CustomTabBarItem view:

func createTabBarItems(_ containers: [CGRect]) {

    var index = 0
    for item in tabBarItems {
        let container = containers[index]

        let customTabBarItem = CustomTabBarItem(frame: container)
        customTabBarItem.setup(item) // HERE I call my setup() method of my Tab Bar Item

        self.addSubview(customTabBarItem)
        customTabBarItems.append(customTabBarItem)

        let button = UIButton(frame: CGRect(x: 0, y: 0, width: container.width, height: container.height))
        button.addTarget(self, action: #selector(CustomTabBar.barItemTapped(_:)), for: UIControlEvents.touchUpInside)

        customTabBarItem.addSubview(button)
        tabBarButtons.append(button)

        index += 1
    }
}
cusmar
  • 1,707
  • 1
  • 16
  • 37
  • `setup()` is being called before the image is laid out. Where are you calling the method? – amariduran Jan 29 '18 at 11:15
  • I edited my first post to answer your question. Does it help? – cusmar Jan 29 '18 at 11:39
  • Inside of `CustomTabBar.swift` do you have `init?(coder aDecoder: NSCoder)` method implemented? And if so, are you calling `super.init(coder: aDecoder)` inside? – amariduran Jan 29 '18 at 11:53
  • I didn't call it, I added it and I still have the same error. It is strange because it work for items images which are not linked to a storyboard reference... – cusmar Jan 29 '18 at 12:00
  • Would you mind providing a link to your project? – amariduran Jan 29 '18 at 12:02

1 Answers1

0

I found something to fix the problem, but I still don't know how to explain it.

What I did: I added a Tab Bar Item in the storyboard which was referenced in my Main storyboard. Then I put my image on this item, and it works correctly.

Problem solved

cusmar
  • 1,707
  • 1
  • 16
  • 37