0

I created a UITabBarController class

I created two view controllers and set the title property for each one

I want to change the size and the color of text label within the tabs

I already set the tab color and frame using

UITabBar.appearance().barTintColor = UIColor.blueColor()

tabBar.frame = CGRectMake(0, 0, view.frame.width, 100)

Now for adjusting the tabs, I tried each of the following

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIColor.whiteColor(), forKey: NSForegroundColorAttributeName), forState: UIControlState.Normal)

That changed the text color to white

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIColor.whiteColor(), forKey: NSForegroundColorAttributeName), forState: UIControlState.Normal)

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(object: UIFont(name: "Helvetica", size: 16.0)!, forKey: NSFontAttributeName), forState: UIControlState.Normal)

That only changed the size of the text, not the color !

UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(objects: [NSForegroundColorAttributeName, NSFontAttributeName], forKeys: [UIColor.whiteColor(), UIFont(name: "Helvetica", size: 16.0)!]), forState: UIControlState.Normal)

// OR

var attributes = NSDictionary(objects: [NSForegroundColorAttributeName, NSFontAttributeName], forKeys: [UIColor.whiteColor(), UIFont(name: "Helvetica", size: 16.0)!])

UITabBarItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)

That last bit did nothing at all, neither setting the color nor the size of the tabs text

Update------

To be more precise, the above code is all tried within the UITabBarController child class in viewWillAppear

Furthermore, I was curious about how to make the whole tabs (tab-able) and not only the text label part of it, and also add some sort of a line separator between the tabs

Ahmed Elashker
  • 1,001
  • 1
  • 8
  • 17

1 Answers1

0

I found out how to make it happen

Setting the title color and size has to go through the UITabBarItem appearance "proxy"

override func viewWillLayoutSubviews() {

    self.tabBar.frame = CGRectMake(0, 20, view.frame.width, 50)

    var font = UIFont(name: "Helvetica", size: 16)

    var objs = NSArray(objects: font!, UIColor.whiteColor())

    var keys = NSArray(objects: NSFontAttributeName, NSForegroundColorAttributeName)

    UITabBarItem.appearance().setTitleTextAttributes(NSDictionary(objects: objs, forKeys: keys), forState: UIControlState.Normal)
}
Ahmed Elashker
  • 1,001
  • 1
  • 8
  • 17