ios - UIKit controller/view to switch between views? -
sorry novice question if is.
i have controller/view uisegmentedcontrol
. need switch between controllers/views let views take rest of screen. idea pretty similar uitabbarcontroller
switching controls not tab buttons segment controls. how can achieve according ios design , practice?
create container viewcontroller , include tis logics:
@property (nonatomic, strong) uisegmentedcontrol *segmentedcontrol; @property (nonatomic, strong) nsmutablearray *viewcontrollers; @property (nonatomic, weak) uiview *containerview; @property (nonatomic) nsinteger currentselectedindex; _segmentedcontrol = [[uisegmentedcontrol alloc] initwithitems:nil]; [_segmentedcontrol addtarget:self action:@selector(changeviewcontroller:) forcontrolevents:uicontroleventvaluechanged]; - (void)changeviewcontroller:(uisegmentedcontrol *)segmentedcontrol { uiviewcontroller *oldviewcontroller = self.viewcontrollers[self.currentselectedindex]; uiviewcontroller *newviewcontroller = self.viewcontrollers[segmentedcontrol.selectedsegmentindex]; [self willtransitiontoviewcontroller:newviewcontroller]; [self transitionfromviewcontroller:oldviewcontroller toviewcontroller:newviewcontroller duration:0 options:uiviewanimationoptiontransitionnone animations:nil completion:^(bool finished) { if (finished) [self didtransitiontoviewcontroller:newviewcontroller]; }]; } - (void)showfirstviewcontroller { uiviewcontroller *firstviewcontroller = [self.viewcontrollers firstobject]; // set required frame firstviewcontroller.view.frame = self.containerview.bounds; firstviewcontroller.view.autoresizingmask = uiviewautoresizingflexiblewidth|uiviewautoresizingflexibleheight; [self.containerview addsubview:firstviewcontroller.view]; [self willtransitiontoviewcontroller:firstviewcontroller]; [self didtransitiontoviewcontroller:firstviewcontroller]; } - (void)willtransitiontoviewcontroller:(uiviewcontroller *)viewcontroller { if (self.currentselectedindex != uisegmentedcontrolnosegment) { uiviewcontroller *oldviewcontroller = self.viewcontrollers[self.currentselectedindex]; [oldviewcontroller willmovetoparentviewcontroller:nil]; } viewcontroller.containerview.frame = self.containerview.bounds; viewcontroller.view.autoresizingmask = uiviewautoresizingflexiblewidth|uiviewautoresizingflexibleheight; } - (void)didtransitiontoviewcontroller:(uiviewcontroller *)viewcontroller { [viewcontroller didmovetoparentviewcontroller:self]; self.segmentedcontrol.selectedsegmentindex = [self.viewcontrollers indexofobject:viewcontroller]; self.currentselectedindex = [self.viewcontrollers indexofobject:viewcontroller]; }
Comments
Post a Comment