Pure code creation, not advocating the use of Xib and story version (storyboard), while improving development speed, but consumes performance.
1. First look at the hierarchical relationship between the controllers: as shown
From this picture can be seen: the right assembled views is presented to the user interface, its left window is the bottom of the windows, the focus is to the left, is the Tab bar View,tab Bar view above the Navigation view, Finally, the user-defined view.
2. After understanding, the code should be very well written. The Navigation view needs to be added to the tab bar view, and tab view is added to the window. is window set Uitabbarcontroller,uitabbarcontroller set Uinavigationcontroller, Uinavigationcontroller set Uiviewcontroller.
3.
1. New single View Application project
For a better understanding, we create a new single View application
2, delete ViewController.h and other 3 files
Delete the 3 files as shown:
ViewController.h, VIEWCONTROLLER.M and Main.storyboard.
3. New Mainviewcontroller
Based on the hierarchy diagram above, we need to add uitabbarcontroller to the window, so here we create a new mainviewcontroller and let it inherit Uitabbarcontroller, as shown in:
Click Next, inherit from Uitabbarcontroller, do not tick also Create XIB file, such as:
4. Modify the APPDELEGATE.M file
In order for Mainviewcontroller to be added to the window, modify the APPDELEGATE.M file directly on the code as follows:
#import"AppDelegate.h" #import "MainViewController.h" @interface appdelegate () @end @implementation appdelegate-(bool) Application: ( uiapplication *) application didfinishlaunchingwithoptions: ( nsdictionary *) launchoptions {//Override point for Customization after application launch. Mainviewcontroller *MAINVC = [[Mainviewcontroller alloc] init]; self.windowreturn yes;}
5. Create a new two tab page
New Firstviewcontroller and Secondviewcontroller, inherited from Uiviewcontroller, here do not tick also create XIB file (these two images are intercepted, no tick, behind all-pure code creation , in order to improve the maintenance of code)
Such as:
Firstviewcontroller:
Secondviewcontroller:
Once created, now the file directory structure is like this
6, modify the MAINVIEWCONTROLLER.M
Now we need to add the page you just created (First/second viewcontroller) to the navigation controller, and then add the navigation controller to the tag controller.
Directly on the code:
#import"MainViewController.h"#import"FirstViewController.h"#import"SecondViewController.h"@interfaceMainviewcontroller ()@end@implementationmainviewcontroller-(void) Viewdidload {[Super Viewdidload];Execute Load Controller [Self loadviewcontrollers];} - (void) Loadviewcontrollers {1. Create a new first Page view Controller instance Firstviewcontroller *FIRSTVC = [[Firstviewcontroller alloc] init];2. Create a new first page navigation controller instance and add the FIRSTVCUinavigationcontroller *FIRSTNC = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:FIRSTVC];3. New tab bar icon for the first page Uitabbaritem *firsttabbaritem =[[uitabbaritem alloc] Initwithtabbarsystemitem: Uitabbarsystemitembookmarks tag:0]; //4. Add the icon to the first page of the navigation controller Firstnc.tabbaritem = Firsttabbaritem; Span class= "hljs-comment" >//second page Secondviewcontroller *SECONDVC = [[Secondviewcontroller alloc] init]; uinavigationcontroller *secondnc = [[1]; Secondnc.tabbaritem = Secondtabbaritem; //5. Create a new array of navigation controller instances nsarray *controllersarray = @[firstNC, SECONDNC]; //6. Add an array of navigation controllers to the tag controller [self setviewcontrollers: Controllersarray animated:yes];}
Read the code comments directly to understand the process: Uiviewcontroller add to Uinavigationcontroller, Uinavigationcontroller join Uitabbarcontroller, Corresponds to the above level diagram.
We can now run it, and we will be prompted with the following error:
' Nsinvalidargumentexception ', Reason: ' Could not find a storyboard named ' Main ' in bundle NSBundle
Look at this hint, it turns out that we removed the Main.storyboard in step 2, but the main interface of the program setting is main, which will definitely be an error. We need to change the configuration of the project, it will be changed to Launchscreen bar, such as:
We run again, should be no problem, the effect has come out, such as:
We add the title of each page to make it easier to see the effect:
Firstviewcontroller.m
- (void)viewDidLoad { [super viewDidLoad]; // 设置title self.title = @"main";}
Secondviewcontroller.m
- (void)viewDidLoad { [super viewDidLoad]; // 设置title self.title = @"second";}
Run again, click on the corresponding label image, you can see the effect:
7. New Detailsviewcontroller
In order to reflect the uinavigaitoncontroller, we need to create a new page, and then add a button in the Firstviewcontroller, click the button to jump to this page, such as:
After the file is created, we add the title to this page:
Detailsviewcontroller.m
- (void)viewDidLoad { [super viewDidLoad]; // 设置title self.title = @"details";}
We then add a button to the Firstviewcontroller and then connect to the FIRSTVIEWCONTROLLER.M file to implement the Click event, code:
-(void) viewdidload {
[Super viewdidload];
additional setup after loading the view.
self. Title = @ "Main";
self. View. BackgroundColor = [uicolor yellowcolor];
UIButton * btn = [[UIButton alloc]initwithframe:cgrectmake( (+- )];
Btn. backgroundcolor = [uicolor greencolor];
[Btn settitle:@ "GO" forstate:uicontrolstatenormal];
[self. View addsubview: btn];
[Btn addTarget:self action:@selector(goclick) forcontrolevents: UIControlEventTouchUpInside];
}
-(void) goclick{
Detailsviewcontroller * detail = [[detailsviewcontroller alloc]init];
Hide Tab Bar
Detail. hidesbottombarwhenpushed = YES;
[self. Navigationcontroller pushviewcontroller:d etail animated:NO];
}
With the code above, click Go to jump to the Detailsviewcontroller page.
Note on line fourth, if we need to hide the tab bar at the bottom after entering the details page, you can set hidesbottombarwhenpushed to Yes here. Note that the call in the Viewdidload method in the DETAILSVIEWCONTROLLER.M file is not effective in viewwillappear, and is not available in the Init method, as in the following code:
- (instancetype)init { self.hidesBottomBarWhenPushed = YES; return [super init];}
Run again, click the Go button and jump to the details page:
In this respect, we have realized the integrated use of Tabbar and navigation.
Beautify Top navigation bar
We can beautify the top navigation bar, such as changing the background, text color, as well as the status bar text color.
Loadviewcontrollers method in the Mainviewcontroller.m file, after creating the FIRSTNC instance, add:
// 设置导航栏背景图片(需要素材) [firstNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg"] forBarMetrics:UIBarMetricsDefault]; // 设置导航栏样式 [firstNC.navigationBar setBarStyle:UIBarStyleBlackTranslucent]; // 设置返回按钮文字和图标颜色 [firstNC.navigationBar setTintColor:[UIColor whiteColor]];
Once you have added the above code, you can see that the text color of the navigation bar and status bar has changed
Go to Details page
Bottom Tab Bar
Of course, the bottom of the tab bar can also change the background, in the Mainviewcontroller.m Viewdidload method:
- (void)viewDidLoad { [super viewDidLoad]; // 执行加载控制器 [self loadViewControllers]; //设置底部标签栏背景 [self.tabBar setBackgroundImage:[UIImage imageNamed:@"bg"]];}
You can also customize the default and selected images for each label, and then copy the images
In Mainviewcontrollers, redefine the first tab bar item:
// 自定义显示的图片 UIImage *homeNormal = [UIImage imageNamed:@"home_normal"]; UITabBarItem *firstTabBarItem = [[UITabBarItem alloc] initWithTitle:@"Main" image:homeNormal tag:0];
The final effect, such as:
Uitabbarcontroller + Uinavigationcontroller Composite Architecture Design