Change TabBarItem title font to bold in Swift for iOS -


i'm trying set font weight of selected tab bar item bold font. seems has no effect. idea wrong. forstate: .normal works expected, forstate: .selected has no effect.

let tabbaritem0 = tabbar.items![0] as! uitabbaritem var selectedimage0 : uiimage = uiimage(named:"ic_tabbar_item_one")!.imagewithrenderingmode(uiimagerenderingmode.alwaysoriginal) var fontlight:uifont = uifont(name: "helveticaneue-ultralight", size: 12)! var fontbold:uifont = uifont(name: "helveticaneue-bold", size: 12)!  tabbaritem0.image = unselectedimage0 tabbaritem0.selectedimage = selectedimage0 tabbaritem0.title = "overview" tabbaritem0.settitletextattributes(     [         nsforegroundcolorattributename: uicolor.whitecolor(),         nsfontattributename: fontlight     ], forstate: .normal)  tabbaritem0.settitletextattributes(     [         nsforegroundcolorattributename: uicolor.whitecolor(),         nsfontattributename: fontbold     ], forstate: uicontrolstate.selected) 

found solution (swift 3, xcode 8.1)

  1. in storyboard, give unique tag each uitabbaritem have: every tab -> select , go it's "attributes inspector" -> give each 1 unique number in "tag" field should not use 0 (i used 1 through 4).

    this sets later, identify tab pressed.


  1. create new subclass of uitabbarcontroller , assign it: file -> new file -> ios cocoa touch -> create subclass of uitabbarcontroller. assign new .swift file uitabbarcontroller under "identity inspector."

    we need custom logic in our uitabbarcontroller.


  1. create new subclass of uitabbaritem, assign same file of uitabbaritems: file -> new file -> ios cocoa touch -> create subclass of uitabbaritem , assign same 1 of tabs.

    we need shared custom logic in our tab bar items.


  1. add code uitabbaritem subclass, sets initial state (primary tab bold, rest unselected) , allow programmatic tab changes well:

    class myuitabbaritemsubclass: uitabbaritem {      //choose initial state fonts , weights here     let normaltitlefont = uifont.systemfont(ofsize: 12, weight: uifontweightregular)      let selectedtitlefont = uifont.systemfont(ofsize: 12, weight: uifontweightbold)      //choose initial state colors here     let normaltitlecolor = uicolor.gray      let selectedtitlecolor = uicolor.black      //assigns proper initial state logic when each tab instantiates      override func awakefromnib() {         super.awakefromnib()          //this tag # should primary tab's tag*          if self.tag == 1 {              self.settitletextattributes([nsfontattributename: selectedtitlefont, nsforegroundcolorattributename: selectedtitlecolor], for: uicontrolstate.normal)         } else {             self.settitletextattributes([nsfontattributename: normaltitlefont, nsforegroundcolorattributename: normaltitlecolor], for: uicontrolstate.normal)         }      }  } 

here set initial state tabs set correctly when app opens up, we'll take care of physical tab presses in next subclass.


  1. add code uitabbarcontroller subclass, it's logic assigning correct states press on tabs.

    class myuitabbarcontrollersubclass: uitabbarcontroller {      //choose normal , selected fonts here     let normaltitlefont = uifont.systemfont(ofsize: 12, weight: uifontweightregular)     let selectedtitlefont = uifont.systemfont(ofsize: 12, weight: uifontweightbold)      //choose normal , selected colors here     let normaltitlecolor = uicolor.gray     let selectedtitlecolor = uicolor.black       //the following delegate method uitabbar protocol that's available      //to uitabbarcontroller automatically. sends information every     //time tab pressed. since tagged our tabs earlier, we'll know 1 pressed,     //and pass identifier function set our button states      override func tabbar(_ tabbar: uitabbar, didselect item: uitabbaritem) {         setbuttonstates(itemtag: item.tag)     }       //the function takes tabbar.tag int     func setbuttonstates (itemtag: int) {          //making array of tabs         let tabs = self.tabbar.items          //looping through , setting states         var x = 0         while x < (tabs?.count)! {              if tabs?[x].tag == itemtag {                 tabs?[x].settitletextattributes([nsfontattributename: selectedtitlefont, nsforegroundcolorattributename: selectedtitlecolor], for: uicontrolstate.normal)             } else {                 tabs?[x].settitletextattributes([nsfontattributename: normaltitlefont, nsforegroundcolorattributename: normaltitlecolor], for: uicontrolstate.normal)             }              x += 1          }      }  } 

it looks such pain because reason tabs not recognize state changes ".selected". had working .normal states - detecting state changes ourselves.


  1. you can programmatically change tabs , still detect state changes by... i'll update later if has interest, ask.

hope helped!


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -