The iOS fancy-change system features a Uinavigationbar navigation bar style that allows you to have a purer, stronger custom navigation bar!!! It ' s amazing

Source: Internet
Author: User

In the development process, if you are using the traditional style top 20px StatusBar plus the following 44 height

Navigationbar, such a product manager is really good, very simple, but now more and more people from

Define the navigation bar, it looks like there is no system so inflexible, but I just do not want to customize, I want to change the system guide

Various properties and frame of the navigation bar.

First, look at what you need to look like.

1.1px pixel at the bottom of the navigation bar I'm going to remove that need a pure

2. Height of navigation bar I want to change to for example 100

3. When the navigation bar height changes, the inside Leftbaritem and Rightbaritem as well as the Titleview also must follow the vertical center


Please look at the picture below

Flawed , defective, and 1px at the bottom.



Another one, and there's no flaw.



It's perfect, clean, neat, and it looks really comfortable, okay?



There are three problems that need to be solved, let's solve each one, I use recursion here, it's not bad.

first question: What is the bottom 1px of the navigation bar, how to keep him from showing

First look at a picture, what is he exactly, I use the reveal opened, you can also use Xcode own


Open as follows, this is the graphical interface


Here is the tree chart, the corresponding control Uiimageview



Yes, that's him, the one with the Uiimageview,frame 0.5, the direct code recursion.

Uiimageview *cancelimageview = [self searchNavigationBarUnderLine:self.navigationController.navigationBar];
    Cancelimageview.hidden = YES;

-(Uiimageview *) Searchnavigationbarunderline: (UIView *) View
{
    //find whether it belongs to Uiimageview and whether the frame is less than 1.0 if
    ([ View Iskindofclass:[uiimageview class]] && view.bounds.size.height < 1.0f)
    {return
        (Uiimageview *) view;
    }
    Loop second Layer lookup for
    (UIView *subview in view.subviews)
    {
        //recursive lookup
        uiimageview *imageview = [self Searchnavigationbarunderline:subview];
        if (ImageView) {return
            ImageView
        }}
    return nil;
}

This line of code recursively finds out if there is a control uiimageview and the height of his frame is less than 1.0, so that the extra 1pxHidden can be



Is it clean, OK, so the first problem is solved.


second question: How to change the height of the navigation bar (this thing is a bit tricky, go to GitHub to find resources)

Jznavigationextension, that's him, to help you find the ready-made, he can change the navigation bar many properties, we first use a change frame

Portal: Click to open Link

Platform:ios, ' 7.0 '

Target ' recursive method to cancel 1px ' do

Pod ' jznavigationextension '

End

And then you just include his head file.

#import "JZNavigationExtension.h"

[Self.navigationcontroller setjz_navigationbarsize:cgsizemake ([UIScreen mainscreen].bounds.size.width, 100)];


It's done, classmate.




Third question: How to center the controls on the navigation bar vertically

naïve I think that after the second problem is solved, you put Uibarbuttonitem and Navigationitem title is the default vertically centered


However, the reality is brutal, internal layoutsubviews do not know what to do, for why not center ...


Now the idea is probably, to layoutsubviews again when the center, the system is not centered, we write a category, call the parent

class, which results in the center of all controls

#import "Uinavigationbar+customheight.h"

@implementation Uinavigationbar (customheight)

-(cgsize) Sizethatfits: (cgsize) Size {
    //change navigation bar height. The height must be even, otherwise there'll be is a white line above the navigation bar.
    Cgsize newsize = Cgsizemake (self.frame.size.width, MB);
    return newsize;
}

-(void) layoutsubviews {[Super layoutsubviews];
    Make items on navigation bar vertically centered.
        int i = 0;
            For (UIView *view in self.subviews) {//NSLog (@ "&LT;%I&GT;.%@", I, [view description]);
            i++;
            Here the 1 is the background imageview, do not need to configure if (i = = 1) continue;
                Here's 3 is Titleview, not set will have Bug,title will not follow the center, need to manually adjust his child view label frame, this is a pit, must pay attention to this write if (i = = 3) { For (Uilabel *label in view.subviews) {float y = (view.bounds.size.height-label.bounds.siz
                    E.height)/2;
                    CGRect rec = label.frame;
                    REC.ORIGIN.Y = y;
                Label.frame = rec;
        } [self Alignverticalcenter:view];
    }-(void) Alignverticalcenter: (UIView *) View {//centered float centery = self.bounds.size.height/2.0f;
    Cgpoint Center = view.center;
  Center.y = CenterY;  View.center = center; }


Here's a way to look at the recursion of all the views of Uinavigationbar, and you can follow the print log so that the inner control layout

will have more understanding

    [Self LogView:self.navigationController.navigationBar];
    
}


-(void) LogView: (UIView *) View
{
    NSLog (@ "<!>,%@", view.description);
    For (UIView *subview in view.subviews)
    {
        [self logview:subview];
    }
}


three problems solved, so you can let your navigation bar to change, remove the bottom 1px, and the height can change at any time, the internal control

Is also centered, anyway, my problem solved, you have any new discoveries can share under


Note: Here we share a uibarbuttonitem spacing and offset problem

This uibarbuttonsystemitemfixedspace property does not have any style, is special to add the gap between the number of positive spacing, the greater the number of negative numbers
    uibarbuttonitem *spaceitem = [ Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target:nil Action:nil];
    Spaceitem.width = m;
    Uibarbuttonitem *rightbarbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemplay Target:self Action: @selector (Clickright)];
    Self.navigationItem.rightBarButtonItems = @[spaceitem,rightbarbutton];

That's how it works.



basically introduced here is over, see the people here, really thank you, I seriously write, you are happy to see, thank you very much



Demo Portal: Click on the Portal download demo


over~~~~~~



Related Article

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.