Welcome to follow my official account. I will regularly share some solutions to the problems I encounter in the project and some practical skills of iOS. At the present stage, I will mainly sort out some basic knowledge and record it

The post will also be synchronized to my blog: ppsheep.com

In daily development, we often encounter some problems about NavigationBar, such as visual design, and often change the style of NavigationBar. Although we can deal with them in viewwillApper, it is always too troublesome and requires a lot of redundant code. Today, we will talk about this effect. In fact, there are many apps that use this effect

Let’s take a look at some apps that already have this effect

This is the effect of the Tmall APP, pay attention to its navigation bar

This is netease news, pay attention to the navigation bar

More and more apps are using this style to control the different styles of the navigation bar, and today we are going to implement this effect.

A third-party library is required

Github.com/rickytan/RT…

With this library we can easily achieve this effect

Create a new project where we use Cocoapods to integrate this third-party library

Integrated RTRootNavigationController

podfile

Xcodeproj 'platform: iOS, xcodeProj' platform: iOS, '8.0' target 'iOS each VC separate a navigation bar' do pod 'RTRootNavigationController' endCopy the code

Use rootController RTRootNavigationController as current

Create BaseViewController

I am here to build a new BaseViewController mainly to introduce RTRootNavigationController, of course, if is OC project, can create a PCH file directly, direct global references to also go, But we usually have a base class ViewController, in the base class, didn’t do any operation, just quote a RTRootNavigationController

#import "RTRootNavigationController.h"

@interface BaseViewController : UIViewController

@endCopy the code

Setting the root Controller

In the Appdelegate, we need to set our window rootcontroller to RTRootNavigationController

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *viewController = [[ViewController alloc] init];
RTRootNavigationController *rootViewController1 = [[RTRootNavigationController alloc] initWithRootViewController:viewController];
_window.rootViewController = rootViewController1;
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
return YES;Copy the code

In ViewController, when we need to push a vc, we need to do this

[self.rt_navigationController pushViewController:vc1 animated:YES complete:nil];Copy the code

Let’s see what happens

Set to return to NavigationBar button

In the current VC, we set the back button, or some other button, which is also convenient

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom]; [btn1 addTarget:self action:@selector(leftBar1Clicked) forControlEvents:UIControlEventTouchUpInside]; [btn1 setTitle: @ "returns 1" forState: UIControlStateNormal]; [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn1 sizeToFit]; UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom]; [btn2 setTitle: @ "return 2" forState: UIControlStateNormal]; [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(leftBar2Clicked) forControlEvents:UIControlEventTouchUpInside]; [btn2 sizeToFit]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:btn2]; self.navigationItem.leftBarButtonItems = @[item1,item2]; UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom]; [btn3 setTitle: @ "right-click" forState: UIControlStateNormal]; [btn3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn3 addTarget:self action:@selector(rightBarClicked) forControlEvents:UIControlEventTouchUpInside]; [btn3 sizeToFit]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:btn3]; self.navigationItem.rightBarButtonItem = rightItem; [self.view addSubview:label];Copy the code

Multiple button definitions are also handy

If you just need a back button on the left that needs a custom style, you can flush the method directly under the current VC

/** If you don't need to do anything about the return event, but you want to customize the return button style, You can override this method directly @param Target listens on an object @param Action returns an event @return custom return button */ -(UIBarButtonItem) *)customBackItemWithTarget:(id)target action:(SEL)action{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; [BTN setTitle: @ "return" forState: UIControlStateNormal]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn sizeToFit]; [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn]; return item; }Copy the code

In this case, don’t set the back button in the upper left corner separately

Skip to VC at the beginning

When we pop, we can pop VC directly at the top of the stack

[self.rt_navigationController popToRootViewControllerAnimated:YES complete:nil];Copy the code

Push to another VC to destroy the current VC

Sometimes we want to achieve the effect that we want to destroy the current VC when the current VCpush is out

ViewController4 *vc4 = [[ViewController4 alloc] init];
[self.rt_navigationController pushViewController:vc4 animated:vc4 complete:^(BOOL finished) {
     [self.rt_navigationController removeViewController:self];
 }];Copy the code

Change the navigation bar color

I forgot to change the color of the navigation bar, so let’s see, to change the color of the navigation bar, I just need to

self.navigationController.navigationBar.barTintColor = [UIColor greenColor];Copy the code

conclusion

If your APP has multiple styles in the navigation bar, you can use this method and it’s very easy to use

Thanks: rickyTan open source github.com/rickytan/RT…

Project source I put: github.com/yangqian111…