UINavigationBar

Here, we first understand the hierarchy diagram of UINavigationController, which helps us to understand UINavigationBar better.

UINavigationController level

So basically, UINavigationController is a container that can hold a bunch of UI view Controllers. How are you going to control all these UI view Controllers? You have to have a tool. The tool is the UINavigationBar. A container is just one bar, which is the console. However, managing so many UIView Controllers, buttons and titles on the console, doesn’t seem boring. To solve this problem, the UINavigationController generates a UINavigationBarItem for each UIViewController. This UINavigationBarItem allows you to change the buttons and titles “above” the console.

In short, the UINavigationBar is a part of the UINavigationController, which is the navigation bar above. UINavigationBar consists of UINavigationItem. UINavigationItem has title, button, prompt text, and so on, which is the title text that we see, the button in the upper right corner.

NavigationItem represents a ViewController in NavigationBar. Specifically, every ViewController added to NavigationController has a corresponding NavigationItem.

A navigation controller controls multiple views, and leftItem,rightItem, and Title on NavigationBar are controlled by the current view controller.

First, basic usage

First, basic usage

self.title = @"TestTitle"; / / same as below. / / the self navigationItem. Title = @ "TestTitle"; rightItemself.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc] initWithTitle:@"Done"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(doneTestAction)]; leftItemself.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(cancelTestAction)];Copy the code

Two, change color

Two, change color

Note that the color change on title is different from the color method on Item

/ / change the color self. NavigationController. NavigationBar. BarTintColor = [UIColorblueColor]; / / change the title color self. NavigationController. NavigationBar. TitleTextAttributes = @ {NSForegroundColorAttributeName: [UIColorredColor] }; / / change the Item color self. NavigationController. NavigationBar. TintColor = [UIColorwhiteColor];Copy the code

We usually use the following method as well, but note that it only works in AppDelegate, or RootController in UINavagaitonController, and only in pure code. Storyboard Settings in the root view have no effect, and any other subview Settings alone have no effect.

[[UINavigationBarappearance] setTintColor:[UIColorwhiteColor]];
[UINavigationBarappearance].titleTextAttributes =@{NSForegroundColorAttributeName: [UIColorwhiteColor]                                                    };
[[UINavigationBarappearance] setBarTintColor:[UIColorblueColor]];
Copy the code

Of course, you can always use pictures to change

[[UINavigationBarappearance] setBackgroundImage:[UIImageimageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImageimageNamed:@"nav"]  forBarMetrics:UIBarMetricsDefault];
Copy the code

3. Hide the navigation bar

self.navigationController.navigationBar.hidden =YES;
Copy the code

The status bar is in the way

But note that sometimes the status bar does not disappear. To solve this problem, you need to use the following problem, mentioned in edgesForExtendedLayout

self.edgesForExtendedLayout =UIRectEdgeNone;
Copy the code

EdgesForExtendedLayout is a property of type UIExtendedEdge that specifies the direction in which the edge should extend. Because iOS7 encourages a full-screen layout, the default is naturally UIRectEdgeAll, with all the edges extending, which means that if you have NavigationBar on top and tabBar on bottom, the view will still extend to cover all the surrounding areas.

The navigation bar disappears dynamically

if(scrollView.contentOffset.y >64)
 {       
    [self.navigationController setNavigationBarHidden:YESanimated:YES];
}else{
    [self.navigationController setNavigationBarHidden:NOanimated:YES];
}
Copy the code

NavigationBarHidden navigationBarHidden navigationBarHidden

In both cases, you can hide the navigation bar and still use push and POP. If you hide the navigationBar with navigationbar. hidden, you can continue to use the sliding pop effect provided by navigationBarHidden. If you use navigationBarHidden, this action will not work. But the former navigationBar.hidden has no automatic animation effect.

Ps handles the status bar:

Note that after iOS 7, we changed the status bar and set the Plist Info View Controller-based Status bar appearance to YES, so the status bar will change according to the configuration of each UIViewController. To change the status bar in UIViewController, you need to override the following two methods:

// Whether the status bar is hidden - (BOOL)prefersStatusBarHidden; // preferredStatusBarStyle (UIStatusBarStyle);Copy the code

If the View Controller-based status bar appearance is NO, the status bar is not under the separate control of UIViewController. Execute setStatusBarHidden where you need to modify.

[UIApplicationsharedApplication].statusBarStyle =UIStatusBarStyleLightContent;
Copy the code

This will make the status bar white, but iOS 9 will still use the same method. Let me rewrite the following method.

- (UIStatusBarStyle)preferredStatusBarStyle;
Copy the code

4. Change of screen origin

I’m not going to compare this to before iOS 7, iOS 6 does start at the bottom of the status bar (0,20), iOS 7 does start at the top left of the status bar (0,0), but sometimes, We also have a (0,64) layout in NavigationController. What happens here? Let’s take a look at the following three properties:

**extendedLayoutIncludesOpaqueBars**
Copy the code

The default value is NO, which specifies whether the view extends to the area of the Bar when the Bar uses an opaque image. Therefore, if we customize the Nav bar background image, the view will start from below the navigation bar.

**edgesForExtendedLayout**
Copy the code

The default is UIRectEdgeAll, which is a full-screen layout (encouraged in iOS7 so that you can see blurry content through the translucent bar). If set to UIExtendedEdgeNone, the view does not extend beyond the bar

**automaticallyAdjustsScrollViewInsets**
Copy the code

The default value is YES. If the View has a unique UIScrollView or its subclass View, it will automatically set the corresponding inner margin. (If there is a Navbar, the inner margin is 64, so that the ScrollView can fill the screen with content below 64 pixels and not be covered. Slide the ScrollView to see the contents of the scrollView through the translucent effect.

So sometimes, if we see that the origin has changed, we can see if any of these properties have changed. Often when we use the tableView or collectionView will need to set up the self. The automaticallyAdjustsScrollViewInsets = NO, don’t let it automatically adjust.

From: www.jianshu.com/p/d8b1ae461…