Summary of common usage of UINavigationBar

In the project, we often need to customize the navigation bar and status bar display style, or display / hide. Here is a summary of common usage.

Set navigation bar title

There are two ways:

  1. Modify
    self.navigationItem.title = @ by learning navigation bar in navigationItem”;
  2. Modify
    self.title = @ by learning navigation bar in ViewController”

Set the background color of the navigation bar

Self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
effect:

Summary of common usage of UINavigationBar
bar_background_color.png

Set navigation bar title font, color

NSDictionary *attributes = @{NSForegroundColorAttributeName cyanColor] NSFontAttributeName: [UIFont systemFontOfSize:: [UIColor, 30]}; [self.navigationController.navigationBar setTitleTextAttributes: attributes];

Effect:

Summary of common usage of UINavigationBar
title_font_and_color.png

Set navigation bar background image

Here for simplicity, draw a picture with color.

UIImage *image1 = [UIImage imageWithColor: [UIColor redColor]]; UIImage *image2 = [UIImage imageWithColor: [UIColor blueColor]] UINavigationBar; *navBar = self.navigationController.navigationBar; [navBar setBackgroundImage: image1 forBarMetrics: UIBarMetricsDefault] [navBar setBackgroundImage: image2 forBarMetrics: UIBarMetricsCompact];

To set the background image, to specify the appropriate picture. UIBarMetricsDefault for vertical screen, UIBarMetricsCompact for horizontal screen.

Set navigation bar for transparency

The navigation bar is a semi transparent effect by default, but if you want to achieve arbitrary transparency of alpha values from 0 to 1, use the following code.

CGFloat alphaValue = 0.2; / / can be set to any value between 0 and 1 UINavigationBar *navBar [navBar setBackgroundImage: [UIImage = self.navigationController.navigationBar; imageWithColor: [UIColor colorWithRed: 0 green: 0 blue: 1 alpha: alphaValue]] forBarMetrics: UIBarMetricsDefault];

If you need to drag the ScrollView process, modify the navigation bar transparency, you need to use the above code.

Hide the line below the navigation bar

Below the navigation bar, the default is a gray line. If you want to hide, you need to set up the same time and backgroundImage and shadowImage two attributes. If you do not set backgroundImage, even if set up shadowImage, it will not take effect.

[self.navigationController.navigationBar setBackgroundImage: new] forBarMetrics: UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [UIImage new] ([UIImage);

Change the color of the separator line below the navigation bar

Since shadowImage assignment for [UIImage New], you can hide the separation line, then we can also according to this principle, to modify the color of the separator.

[self.navigationController.navigationBar setBackgroundImage: [UIImage new] forBarMetrics: UIBarMetricsDefault] [UIImage imageWithColor: [UIColor; self.navigationController.navigationBar.shadowImage = redColor]];

Add a hint text to the title of the navigation bar

Self.navigationItem.prompt = "Navigation prompts appear at @ the top." UIImage [UIImage imageWithColor: [UIColor; *image1 = yellowColor]]; UIImage = *image2 [UIImage imageWithColor: [UIColor purpleColor]] UINavigationBar; *navBar = self.navigationController.navigationBar; [navBar setBackgroundImage: image1 forBarMetrics: UIBarMetricsDefaultPrompt] [navBar setBackgroundImage: image2 forBarMetrics: UIBarMetricsCompactPrompt];
Summary of common usage of UINavigationBar
prompt.png

in the case of the setting of the prompt, the height of the navigation bar will become larger. Correspondingly, the addition of two kinds of BarMetrics:UIBarMetricsDefaultPrompt and UIBarMetricsCompactPrompt, respectively, corresponding to the vertical and horizontal screen.

Set the buttons on the left and right of the navigation bar

UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle: @ style: UIBarButtonItemStylePlain target: self "return" action: @selector (onBack)]; UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle: @ UIBarButtonItemStylePlain target: self "off" style: action: @selector (onClose)]; self.navigationItem.leftBarButtonItems = @[backItem, closeItem]; UIBarButtonItem *right1 = [[UIBarButtonItem alloc] initWithTitle: @ 1 UIBarButtonItemStylePlain target: nil right style: action: nil]; UIBarButtonItem *right2 = [[UIBarButtonItem alloc] initWithTitle: @ 2 UIBarButtonItemStylePlain target: nil right style: action: nil]; self.navigationItem.rightBarButtonItems = @[right1, right2];

Set navigation bar title to view

UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:@[@ "boy", "girl", @ @ "all" "; [segControl setSelectedSegmentIndex:0]; self.navigationItem.titleView = segControl;
Summary of common usage of UINavigationBar

Set the render color for the navigation bar

UINavigationBar tintColor attributes can be used to modify the return button, as well as the color of the text on the left and right buttons.

Self.title = NSStringFromClass ([self class]); self.navigationController.navigationBar.tintColor [UIColor = yellowColor]; UIBarButtonItem = *right1 [[UIBarButtonItem alloc] initWithTitle: style: UIBarButtonItemStylePlain @ "1 right" target: nil action: nil]; UIBarButtonItem *right2 = [[UIBarButtonItem alloc] initWithTitle: @ 2 UIBarButtonItemStylePlain target: nil right style: action: nil]; self.navigationItem.rightBarButtonItems = @[right1, right2];
Summary of common usage of UINavigationBar
tintColor.png

Hide navigation bar

Self.navigationController.navigationBarHidden = YES;

Modify return button

Method 1: modify the UINavigationItem leftBarButtonItem (s)

UIImage *leftButtonIcon = [[UIImage imageNamed:@ imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] UIBarButtonItem "titlebar_back_normal"]; *leftButton = [[UIBarButtonItem alloc] initWithImage:leftButtonIcon style:UIBarButtonItemStylePlain target:self action:@selector (goToBack)]; UIBarButtonItem *negaSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negaSpacer.width = -20.f; self.navigationItem.leftBarButtonItems = @[negaSpacer, leftButton];
Summary of common usage of UINavigationBar
backbutton.png

the benefit of this approach is that you can set the button function returns a response; but it also poses a problem, is from the left edge of the screen, slide to the right, not to return to the higher level interface, which is the right sliding failure gesture. The solution is to set self.navigationController.interactivePopGestureRecognizer.delegate to nil. after a push of ViewController

TestBackButton *vc = [TestBackButton pushViewController:; [self.navigationController VC animated: YES]; self.navigationController.interactivePopGestureRecognizer.delegate = nil; new];

Method 2: set the return button of the picture, the title is set to an empty string

UIImage *backButtonBackgroundImage [UIImage imageNamed:@ = "Menu"]; ID appearance = [UIBarButtonItem appearanceWhenContainedIn:[CustomBackButtonNavController class] nil] [appearance setBackButtonBackgroundImage:backButtonBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; empty backBarButton to; / / Provide an hide the'Back'text present by default in the back / / button. / / NOTE: / / You do not need to provide a target or action. These are set by the navigation / / bar. / / NOTE: Setting the title of this bar button item to' (space) works a bug in iOS / / around 7.0.x where the background image would be compressed if the back / / horizontally button title is empty. UIBarButtonItem *backBarButto N = [[UIBarButtonItem initWithTitle:@ style:UIBarButtonItemStylePlain target:nil action:NULL]; self.navigationItem.backBarButtonItem = backBarButton alloc];

This method does not affect the right – hand gesture, but the response function of the return button is built-in and cannot be modified, even if you specify target and action. It is also important to note that if the view controller A is set to backBarButtonItem, the
is used only when the A jumps to B. This is different from leftBarButtonItem (s) and rightBarButtonItem (s).

Set status bar style and hide

In iOS7 and later versions, apple recommends the use of ViewController based programs, UIApplication class setStatusBarStyle, setStatusBarHidden and other methods will be abandoned. By adding UIViewControllerBasedStatusBarAppearance in Info.plist and setting it to YES, APP declares that it uses the ViewController based solution.

Summary of common usage of UINavigationBar
InfoPlist.png

Override two methods in your view controller class:

- (UIStatusBarStyle) preferredStatusBarStyle {return UIStatusBarStyleLightContent;} (BOOL) prefersStatusBarHidden {return YES;}

It should be noted, however, that if the view is embedded within the view controller of the container class, such as UINavigationController, the framework will call the above two methods of the container view controller. So you need to define a subclass of UINavigationController, rewrite the above two methods.

- (UIStatusBarStyle) preferredStatusBarStyle {return self.topViewController.preferredStatusBarStyle;} - (BOOL) prefersStatusBarHidden {return self.topViewController.prefersStatusBarHidden;}

Concluding remarks

Personal level is limited, if there are errors in the text, please correct me.