0

I have a custom Tab Bar class. It is shown below. In main.storyboard, I set the class of my tab bar to be CustomTabBar:

class CustomTabBar: UITabBar {   
    override var items: [UITabBarItem]? //[This line returns an error]
    // items?[0].selectedImage = UIImage(named: "MyImage.png")
    override func sizeThatFits(_ size: CGSize) -> CGSize {
        var size = super.sizeThatFits(size)
        size.height = 49.0
        return size
    }
}

The line "override var items" returns the error "Cannot override with a stored property 'items'." I want to access this property so that I can change associated with the first tab bar item. I want to be able to use the line that is currently commented out below that line. How come I get this error? Also, is there another way to accomplish what I want to do?

Everett
  • 387
  • 1
  • 4
  • 17
  • you do not have to override the items property to add an element into it. why do you want to override it? You can use like super.items?[0].selectedImage = UIImage(named: "MyImage.png") – KingHodor Dec 15 '18 at 19:03
  • I will try this – Everett Dec 15 '18 at 19:35
  • I put super.items?[0].selectedImage = UIImage(named: "MyImage.png") into my CustomTabBar. It then gives an error "Expected Declaration." So I add var items: [UITabBarItem]? to my CustomTabBar. Then it gives the error "Cannot override with stored property 'items'." – Everett Dec 15 '18 at 19:40

1 Answers1

1

Cannot override with a stored property 'items'.

I'm not sure why you want to do this. If you want to set items you can use method setItems(_:animated:) and also when you need to get selected item you can use property called selectedItem.

You can also do other things like:

  • Append new item items?.append()
  • Change properties of item with certain index items?[0].selectedImage = UIImage(named: "MyImage.png")

So for your case, you can say change this items properties in viewDidLoad in your custom TabBarController

override func viewDidLoad() {
    super.viewDidLoad()
    tabBar.items?[0].selectedImage = UIImage(named: "MyImage.png")
}
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • I will try this – Everett Dec 15 '18 at 19:35
  • I use override func setItems(_ items: [UITabBarItem]?, animated: Bool) within my CustomTabBar. Within this function, I have items?[0].image = UIImage(named: "MyImage.png"). However, no image appears in the TabBar. What am I doing wrong? – Everett Dec 15 '18 at 19:38
  • @Everett no, don't override set items. Override init as I described – Robert Dresler Dec 15 '18 at 19:39
  • Your code "override init" above returns the error: "'required' initializer 'init(coder:)' must be provided by subclass of UITabBar." If I add "required init?(coder aDecoder: NSCoder)", as the error suggests, there is a "fatalerror" inside that block. – Everett Dec 15 '18 at 21:39
  • I have added this required init. When the project is run, it gives the error "Thread 1: Fatal error: init(coder:) has not been implemented." – Everett Dec 15 '18 at 21:45
  • With your code above, the items for the tabBar all appear on the very left of the tab bar, instead of evenly spaced at the middle of the tab bar as they are usually. – Everett Dec 15 '18 at 22:07