iOS Development Uinavigation Series one--navigation bar Uinavigtionbar

Source: Internet
Author: User

iOS Development Uinavigation Series one--navigation bar UinavigtionbarFirst, the use of navigation bar

In iOS development, we usually use the navigation controller, which encapsulates a uinavigationbar in the navigation controller, and in fact, we can use the navigation bar alone without using the navigation controller, and in Uinavigationbar, there are many properties that we can customize. , it is very convenient to use.

Ii. creation of Uinavigationbar and style types

The navigation bar inherits from the UIView, so we can create a navigation bar like a normal view, such as creating a navigation bar with a height of 80 and placing it on the Viewcontroller head with the following code:

Uinavigationbar *bar = [[Uinavigationbar alloc]initwithframe:cgrectmake (0, 0, 320, 80)]; [Self.view Addsubview:bar];

The effect is as follows:


We can also set the style properties of the navigation bar, from the iOS6, the Uinavigationbar default to a translucent style, from the above can be seen, the white navigation bar below a hint of red background. The style properties of the navigation bar can be set by the following properties:

@property (nonatomic,assign) Uibarstyle Barstyle;

Uibarstyle is an enumeration in which most of the styles are deprecated, with only the following two effects:

typedef ns_enum (Nsinteger, uibarstyle) {Uibarstyledefault = 0,//default Uibarstyleblack = 1,//Black}

The default style is the white style we see above, the black style effect Rui as follows:

Third, navigation bar common properties and methods

From the above we can see that the navigation bar by default is semi-transparent, we can set this property by the bool value below, set to No, the navigation bar is not transparent, the default is yes: IOS6

@property (nonatomic,assign,getter=istranslucent) BOOL translucent;

The following methods are used to set the color-related properties of the Navigationbar and item above:

@property (null_resettable, Nonatomic,strong) Uicolor *tintcolor;

Tintcolor This property affects the pattern color and font color of the pop button on the left side of the navigation bar, the system defaults to the following color:

@property (Nullable, Nonatomic,strong) Uicolor *bartintcolor;

The Bartintcolor is used to set the background color of the navigation bar, and when this property is set, the translucent effect is invalidated:

-(void) SetBackgroundImage: (Nullable UIImage *) backgroundimage forbarmetrics: (uibarmetrics) Barmetrics Ns_available_ IOS (5_0) ui_appearance_selector;-(Nullable UIImage *) Backgroundimageforbarmetrics: (uibarmetrics) barmetrics;

The above two methods are used to set and get the background pattern of the navigation bar, it is important to note that the default background pattern is not scaled, so we use the image size to match the navigation bar size, there is a uibarmetrics parameter, this parameter sets the state of the device, as follows:

typedef ns_enum (Nsinteger, uibarmetrics) {uibarmetricsdefault,//Normal vertical screen state uibarmetricscompact,//horizontal screen status};
Set the shadow picture of the navigation bar @property (nullable, Nonatomic,strong) UIImage *shadowimage;//Set the title of the navigation bar font Properties @property (nullable,nonatomic , copy) nsdictionary<nsstring *,id> *titletextattributes;

The title Font property affects the middle title of the navigation bar, as follows:

Bar.titletextattributes = @{nsforegroundcolorattributename:[uicolor Redcolor]};

We can also set the vertical position offset of the navigation bar header by the following properties:

-(void) Settitleverticalpositionadjustment: (cgfloat) Adjustment forbarmetrics: (uibarmetrics) barMetrics;-(CGFloat) Titleverticalpositionadjustmentforbarmetrics: (uibarmetrics) barmetrics;

There is also a detail, the pattern of the pop button on the left side of the navigation bar is an arrow by default, and we can modify it using the following method:

@property (Nullable,nonatomic,strong) UIImage *backindicatorimage; @property (nullable,nonatomic,strong) UIImage * Backindicatortransitionmaskimage;
Iv. Push and pop operations for item in navigation bar

Uinavigationbar above is not just a simple display of the title, it also will be the title of the stack management, each of the abstract title of the object in the iOS system is the Uinavigationitem object, we can use push and pop operations to manage the item group.

Add an item to the stack, the previous item will be pushed to the left of the navigation bar, and the pop button will have an animated effect-(void) Pushnavigationitem: (Uinavigationitem *) Item animated: ( BOOL) Animated;//pop a item-(nullable Uinavigationitem *) popnavigationitemanimated: (bool) animated; The current push to the topmost item@property (nullable, Nonatomic,readonly,strong) Uinavigationitem *topitem;//is second only to the top item, The general type is pushed to the left of the navigation bar Item@property (nullable, Nonatomic,readonly,strong) Uinavigationitem *backitem;// Gets the array of all item in the stack @property (nullable,nonatomic,copy) Nsarray<uinavigationitem *> *items;//set a set of item-(void) Setitems: (Nullable nsarray<uinavigationitem *> *) Items animated: (BOOL) animated;
Wu, Uinavigationbardelegate

In Uinavigationbar, there is one of the following properties:

@property (Nullable,nonatomic,weak) id<uinavigationbardelegate> delegate;

With proxies, we can monitor some push and pop operations on the navigation bar:

The item is to be called when the push is returned, no is push-(BOOL) Navigationbar: (Uinavigationbar *) Navigationbar Shouldpushitem: ( Uinavigationitem *) Item; Item has been push after call-(void) Navigationbar: (Uinavigationbar *) Navigationbar Didpushitem: (Uinavigationitem *) item; Item will be called when pop, return no, cannot pop-(BOOL) Navigationbar: (Uinavigationbar *) Navigationbar Shouldpopitem: (Uinavigationitem *) Item Item has been called after pop-(void) Navigationbar: (Uinavigationbar *) Navigationbar Didpopitem: (Uinavigationitem *) item;


iOS Development Uinavigation Series one--navigation bar Uinavigtionbar

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.