The following view (multiple-view sliding-click Switch) is useful in many apps. Using encapsulated view, external call to an API can be achieved, very convenient.
Git code: Https://github.com/HelloYeah/HYTabbarView.
Add a child controller
The code is as follows |
Copy Code |
-(void) Addsubitemwithviewcontroller: (uiviewcontroller *) Viewcontroller;
|
1. External transmission of a controller
The code is as follows |
Copy Code |
-(void) Addsubitemwithviewcontroller: (uiviewcontroller *) viewcontroller{
Uibutton*btn=[uibuttonbuttonwithtype:uibuttontypecustom];
[self.tabbar addsubview:btn]; [Self setupBtn:btnwithTitle:viewController.title];
[Btn addtarget:self Action: @selector (itemselected:) forcontrolevents:uicontroleventtouchupinside];
[Self.subviewcontrollers Addobject:viewcontroller]; }
|
2.KVO listens for the ordinal value of the currently selected view
The code is as follows |
Copy Code |
Nsinteger min = HYCOLUMN/2; if (_selectedindex = self.titles.count-min) { UIButton * tempbtn = self.titles[self.titles.count-min-1]; CGFloat btnx = (hycolumn% 2)? Tempbtn.center.x: (tempbtn.center.x + btn.frame.size.width * 0.5); CGFloat OffsetX = _tabbar.center.x-btnx; [UIView animatewithduration:0.25 animations:^{ _tabbar.contentoffset = Cgpointmake (-offsetX, 0); }]; }else if (_selectedindex > Min && _selectedindex < self.titles.count-min && Self.titles.count > Hycolumn) { CGFloat btnx = (hycolumn% 2)? Btn.center.x: (btn.center.x-btn.frame.size.width * 0.5); CGFloat OffsetX = _tabbar.center.x-btnx; [UIView animatewithduration:0.25 animations:^{ _tabbar.contentoffset = Cgpointmake (-offsetX, 0); }]; } else { [Super Observevalueforkeypath:keypath Ofobject:object Change:change Context:context]; } } |