ios - Switch tab bar view in subview programmatically (custom tab bar controller) -
because want redesign tab bar ui, wrote custom tab bar controller according https://github.com/codepath/ios_guides/wiki/creating-a-custom-tab-bar
in tabbarviewcontroller
's viewdidload(), define several subviews corresponding each tab bar
homeviewcontroller = storyboard.instantiateviewcontrollerwithidentifier("homeviewcontroller") ... viewcontrollers = [homeviewcontroller, searchviewcontroller, accountviewcontroller, trendingviewcontroller]
and main method when tapping tab
@ibaction func didpresstab(_ sender: uibutton) { let previousindex = selectedindex selectedindex = sender.tag tabbuttons[previousindex!].isselected = false let previousvc = viewcontrollers[previousindex!] // remove previous vc previousvc.willmove(toparentviewcontroller: nil) previousvc.view.removefromsuperview() previousvc.removefromparentviewcontroller() // set current vc sender.isselected = true let vc = viewcontrollers[selectedindex] addchildviewcontroller(vc) // adjust size match content view vc.view.frame = contentview.bounds contentview.addsubview(vc.view) vc.didmove(toparentviewcontroller: self) }
i set default tab bar index selectedindex
when tab bar view loaded. however, how can switch next tab bar in homeviewcontroller (without tapping tab bar buttons)?
this doesn't work in homeviewcontroller
tabbarviewcontroller().tabbuttons[2].isselected = true tabbarviewcontroller().didpresstab(tabbarviewcontroller().tabbuttons[2])
i'm not sure how running tab controller, set selectedindex, , update subview in subview controllers.
all need call tabbar.setselectedviewcontroller: , pass view controller.
if know tab index, call tabbar.viewcontrollers[index] , view controller.
Comments
Post a Comment