Navigaitonbar's custom settings Navigationbar introduced:
Navigationbar is a navigation bar above the Navigation view controller.
How do I set this navigationbar? First, let's explore how to get navigationbar.
We can use the following two methods to get the navigationbar of our current page
Self.navigationcontroller.navigationbar[uinavigationbar appearance]
got thisAfter Navigationbar, we can make a custom setting on this property:
To set the bar background color method:
[Self.navigationbar Setbackgroundcolor:[uicolor Redcolor];
To set the bar color:
[Self.navigationbar Setbartintcolor:[uicolor Redcolor];
To set the bar background picture method:
[Self.navigationbar setbackgroundimage:[uiimage imagenamed:@ "Ellipse 4"] forbarmetrics:uibarmetricsdefault];
To set the properties of the title of Navigationbar, we can use thesettitletextattributesmethod to set the.
Uicolor *color = [Uicolor yellowcolor]; Nsshadow *shadow = [[Nsshadow alloc] init]; [Shadow Setshadowcolor:[uicolor Bluecolor]; [Shadow Setshadowoffset:cgsizemake (1, 1)]; Uifont *font = [Uifont fontwithname:@ "Helveticaneue-condensedblack" size:22]; [Self.navigationbar settitletextattributes:[nsdictionary Dictionarywithobjectsandkeys:color, Nsforegroundcolorattributename, shadow, Nsshadowattributename, Font, nsfontattributename, nil]];
In this method, we first use the Nsdictionary value and key to represent the title property. Then extract these attributes from this method and set them to the TitleText. Here we need to note that this method is to override the settings, that is, we set a font first, and then we later use this method to set the shadow, then this font setting is not effective.
Here is the name of our property, which we can easily view at the time of writing, to facilitate the setting of properties:
nsforegroundcolorattributename//Set Font Color nsshadowattributename//set Shadow nsfontattributename//set font
Description of the shadow setting in the above method:
Nsshadow *shadow = [[Nsshadow alloc] init];// Set the color of the shadow [Shadow Setshadowcolor:[uicolor bluecolor]];// Sets the position of the shadow, which is the coordinates of the original position of the shadow offset [Shadow Setshadowoffset:cgsizemake (1, 1)];
How to set the buttons on the Vavigationbar:
Here is a way to set a button in Navigationbar, where it is important to note that the method of setting the button is not valid in Rootviewcotroller in Navigatoncontroller.
The code is set up in the following ways:
Uibarbuttonitem *buttonitem3 = [[Uibarbuttonitem alloc] init]; [ButtonItem3 settitle:@ "Back1"]; [ButtonItem3 settarget:self]; [ButtonItem3 setaction: @selector (backbuttonaction)]; Self.navigationItem.leftBarButtonItem = buttonItem3;
The button on the left is the one we just created.
We can also create several buttons in code:
Uibarbuttonitem *buttonitem2 = [[Uibarbuttonitem alloc] init]; [ButtonItem2 settitle:@ "Back"]; [ButtonItem2 settarget:self]; [ButtonItem2 setaction: @selector (RightBarButton2)]; Uibarbuttonitem *buttonitem1 = [[Uibarbuttonitem alloc] init]; [ButtonItem1 settitle:@ "Left1"]; [ButtonItem1 setwidth:10]; [ButtonItem1 setimage:[uiimage imagenamed:@ "Ellipse 4"]; [ButtonItem1 settarget:self]; [ButtonItem1 setaction: @selector (RightBarButton1)]; Self.navigationItem.rightBarButtonItems = @[buttonitem1, buttonItem2];
The two button on the right is the one we just created.
We can also customize the view with the title location, such as the following code:
UIButton *BUTTONITEM5 = [[UIButton alloc] Initwithframe:cgrectmake (+, A, ())]; [BUTTONITEM5 Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [BUTTONITEM5 settitle:@ "test" forstate:uicontrolstatenormal]; [ButtonItem5 addtarget:self Action: @selector (Titleviewbutton) forcontrolevents:uicontroleventtouchupinside]; Self.navigationItem.titleView = BUTTONITEM5;
look at the nature of this code, Self.navigationItem.titleView is actually the system to provide me with a UIView type, so we can set an instance of the UIView type (or its subclass), set it up to add it to the Self.navigationItem.titleView attributes, so it seems that the custom degree of freedom of this property is very large. For example, just the code: the UIButton that we set in the code is a subclass of UIView. Therefore, we can use this button to set this property.
Custom settings for Navigaitonbar