Multiple sub-controllers in the same interface switch views

Source: Internet
Author: User
Tags set background

Look at the example first:

Essenceviewcontroller is the parent controller.
Allviewcontroller = "All
Videoviewcontroller = "video
Voiceviewcontroller = "Sound
Pictureviewcontroller = "Picture
Wordviewcontroller = "Satin

First, analysis
1. Top menu Bar
The large UIView contains a sub-uiview (menu-selected bottom indicator) and 5 menu UIButton.
2. The intermediate content area is Uiscrollview. Click on the different menu switch sub-controller, scroll ScrollView also want to switch, and the menu bar also follow the switch.

Second, detailed look at the code

////ESSENCEVIEWCONTROLLER.M#Import "EssenceViewController.h"#Import "RecommendTagsViewController.h"#Import "AllViewController.h"#Import "VideoViewController.h"#Import "VoiceViewController.h"#Import "PictureViewController.h"#Import "WordViewController.h"@interfaceEssenceviewcontroller () <UIScrollViewDelegate>/** * Label Bottom RED indicator */@property(Nonatomic,weak) UIView *indicatorview;/** * Buttons inside the top label currently selected * /@property(Nonatomic,weak) UIButton *selectedbutton;/** * Top all Tab view * /@property(Nonatomic,weak) UIView *titlesview;/** * Content View * /@property(Nonatomic,weak) Uiscrollview *contentview;@end@implementationessenceviewcontroller-(void) Viewdidload {[SuperViewdidload];//Set navigation bar[Self setupnav];//Initialize all of the child controllers[Self setupchildvces];//Set the tab bar at the top[Self setuptitlesview];//Bottom Cententview[Self setupcontentview];}/** * Initialize all sub-controllers * /- (void) setupchildvces{Allviewcontroller *all = [[Allviewcontroller alloc] init];    [Self addchildviewcontroller:all];    Videoviewcontroller *video = [[Videoviewcontroller alloc]init];    [Self addchildviewcontroller:video];    Voiceviewcontroller *voice = [[Voiceviewcontroller alloc] init];    [Self addchildviewcontroller:voice];    Pictureviewcontroller *picture = [[Pictureviewcontroller alloc] init];    [Self addchildviewcontroller:picture];    Wordviewcontroller *word = [[Wordviewcontroller alloc] init]; [Self Addchildviewcontroller:word];}/** * Bottom Cententview * /- (void) setupcontentview{//Do not automatically adjust the insetSelf.automaticallyadjustsscrollviewinsets = NO;    Uiscrollview *contentview = [[Uiscrollview alloc] init];    Contentview.frame = Self.view.bounds;    Contentview.delegate = self;    contentview.pagingenabled = YES; [Self.view Insertsubview:contentview Atindex:0]; Contentview.contentsize = Cgsizemake (Contentview.width * Self.childViewControllers.count,0); Self.contentview = Contentview;//Add view for first controller[Self scrollviewdidendscrollinganimation:contentview];}/** * Set the top tab bar */- (void) setuptitlesview{//tab bar overallUIView *titlesview = [[UIView alloc] init]; Titlesview.backgroundcolor = [[Uicolor Whitecolor] Colorwithalphacomponent:0.7];    Titlesview.width = Self.view.width; Titlesview.height = *; TITLESVIEW.Y = -;    [Self.view Addsubview:titlesview]; Self.titlesview = Titlesview;//Bottom Red indicatorUIView *indicatorview = [[UIView alloc] init];    Indicatorview.backgroundcolor = [Uicolor Redcolor]; Indicatorview.height =2; Indicatorview.tag =-1;    Indicatorview.y = Titlesview.height-indicatorview.height; Self.indicatorview = Indicatorview;//Internal Sub-labelNsarray *titles = @[@"All",@"Video",@"Sound",@"Pictures",@"Satin"];    CGFloat height = titlesview.height; CGFloat width = titlesview.width/titles.count; for(Nsinteger i=0; i<titles.count;        i++) {UIButton *button = [[UIButton alloc] init];        Button.tag = i;        Button.height = height;        Button.width = width;        button.x = i * button.width;        [Button settitle:titles[i] forstate:uicontrolstatenormal]; [Button layoutifneeded];//Force layout (Force update of the child control's frame)[Button Settitlecolor:[uicolor Graycolor] forstate:uicontrolstatenormal];        [Button Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatedisabled]; Button.titleLabel.font = [Uifont systemfontofsize: -]; [Button addtarget:self action:@selector(Titleclick:) forcontrolevents:uicontroleventtouchupinside]; [Titlesview Addsubview:button];//Click the first button by default        if(i = =0) {button.enabled = NO; Self.selectedbutton = button;//Let the label inside the button calculate the size according to the text content[Button.titlelabel SizeToFit];            Self.indicatorView.width = Button.titleLabel.width;        Self.indicatorView.centerX = Button.centerx; }    }//Indicatorview last added to Titlesview    //For the back from the Titlesview to take the button convenient[Titlesview Addsubview:indicatorview];}/** * Click the button in the tab bar */- (void) Titleclick: (UIButton *) button{//Modify the state of the buttonself.selectedButton.enabled = YES;    button.enabled = NO; Self.selectedbutton = button;//Let the tag perform animation[UIView animatewithduration:. 025animations:^{self.indicatorView.width = self.selectedButton.titleLabel.width;    Self.indicatorView.centerX = Self.selectedButton.centerX; }];//Rolling ContentviewCgpoint offset = Self.contentView.contentOffset;    Offset.x = Button.tag * self.contentView.width; [Self.contentview Setcontentoffset:offset animated:yes];}/** * Set navigation bar * /- (void) setupnav{//Set navigation bar titleSelf.navigationItem.titleView = [[Uiimageview alloc] Initwithimage:[uiimage imagenamed:@"Maintitle"]];//Set the button on the left side of the navigation barSelf.navigationItem.leftBarButtonItem = [Uibarbuttonitem itemwithimage:@"Maintagsubicon"highimage:@"Maintagsubiconclick"Target:self Action:@selector(Tagbuttonclick)];//Set background colorSelf.view.backgroundColor = Globalbgcolor;}/** * Click the button on the left side of the navigation bar */- (void) tagbuttonclick{Recommendtagsviewcontroller *VC = [[Recommendtagsviewcontroller alloc] init]; [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes];} #pragma mark-<UIScrollViewDelegate>/** * This method is called when scrolling is complete (if not manually dragged ScrollView causes scrolling to complete.- (void) Scrollviewdidendscrollinganimation: (Uiscrollview *) scrollview{//Current indexNsinteger index = scrollview.contentoffset.x/scrollview.width;//Remove sub-controllerUitableviewcontroller *VC = Self.childviewcontrollers[index];    vc.view.x = Scrollview.contentoffset.x; VC.VIEW.Y =0;//Set the Y value of the controller view to 0 (by default)Vc.view.height = Scrollview.height;//Set the height value of the controller view to the entire screen (the default is less than the screen height)    //Set inner marginCGFloat top = Cgrectgetmaxy (Self.titlesView.frame);    CGFloat bottom = self.tabBarController.tabBar.height; Vc.tableView.contentInset = Uiedgeinsetsmake (Top,0, Bottom,0);//Set the inner margin of the scroll barVc.tableView.scrollIndicatorInsets = Vc.tableView.contentInset; [ScrollView AddSubview:vc.view];}/** * Execute when ScrollView stops sliding * /- (void) Scrollviewdidenddecelerating: (Uiscrollview *) scrollview{[self scrollviewdidendscrollinganimation:scrollview];//Click the menu buttonNsinteger index = scrollview.contentoffset.x/scrollview.width; [Self titleclick:self.titlesview.subviews[index]];}@end

Third, other
The above code also uses a uiview category:

////Uiview+extension.h//Package Frame modification#import <UIKit/UIKit.h>  @interface UIView (Extension)@property(nonatomic,Assign)cgsizeSize@property(nonatomic,Assign)CGFloatWidth@property(nonatomic,Assign)CGFloatHeight@property(nonatomic,Assign)CGFloatX@property(nonatomic,Assign)CGFloatY@property(nonatomic,Assign)CGFloatCenterX;@property(nonatomic,Assign)CGFloatCenterY;/* Declares @property in the taxonomy, generates only the declaration of the method, does not generate the implementation of the method and the member variable with the _ Underscore */@end
////UIVIEW+EXTENSION.M#import "Uiview+extension.h"  @implementation UIView (Extension)- (void) SetSize: (cgsize) Size {CGRectframe = Self. Frame; Frame. Size= size; Self. Frame= frame;} - (cgsize) Size {return  Self. Frame. Size;} - (void) SetWidth: (CGFloat) Width {CGRectframe = Self. Frame; Frame. Size. Width= width; Self. Frame= frame;} - (CGFloat) Width {return   Self. Frame. Size. Width;} - (void) SetHeight: (CGFloat) Height {CGRectframe = Self. Frame; Frame. Size. Height= height; Self. Frame= frame;} - (CGFloat) Height {return  Self. Frame. Size. Height;} - (void) SetX: (CGFloat) x {CGRectframe = Self. Frame; Frame. Origin. x= x; Self. Frame= frame;} - (CGFloat) x {return  Self. Frame. Origin. x;} - (void) Sety: (CGFloat) y {CGRectframe = Self. Frame; Frame. Origin. Y= y; Self. Frame= frame;} - (CGFloat) y {return   Self. Frame. Origin. Y;} - (void) Setcenterx: (CGFloat) centerx{CgpointCenter = Self. Center; Center. x= CenterX; Self. Center= center;} - (CGFloat) centerx{return  Self. Center. x;} - (void) Setcentery: (CGFloat) centery{CgpointCenter = Self. Center; Center. Y= CenterY; Self. Center= center;} - (CGFloat) centery{return  Self. Center. Y;}@end

Multiple sub-controllers in the same interface switch views

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.