There are Leftbarbuttonitem and Rightbarbuttonitem in the iOS navigation bar, and we can customize these two uibarbuttonitem according to our requirements.
Four methods of creating
The system provides four ways to create:
Copy Code code as follows:
-(Instancetype) Initwithbarbuttonsystemitem: (Uibarbuttonsystemitem) Systemitem target: (ID) Target action: (SEL) Action
-(Instancetype) Initwithimage: (UIImage *) Image style: (Uibarbuttonitemstyle) style target: (ID) Target action: (SEL) Action
-(Instancetype) Initwithtitle: (NSString *) Title style: (Uibarbuttonitemstyle) style target: (ID) Target action: (SEL) Action
-(Instancetype) Initwithbarbuttonsystemitem: (Uibarbuttonsystemitem) Systemitem target: (ID) Target action: (SEL) Action
-(Instancetype) Initwithcustomview: (UIView *) CustomView;
created by System Uibarbuttonsystemitem
Custom Rightbarbuttonitem, code as follows:
Copy Code code as follows:
Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemdone target:self Action: @selector (right:)];
Uibarbuttonsystemitem has the following styles to choose from:
Copy Code code as follows:
typedef ns_enum (Nsinteger, Uibarbuttonsystemitem) {
Uibarbuttonsystemitemdone,
Uibarbuttonsystemitemcancel,
Uibarbuttonsystemitemedit,
Uibarbuttonsystemitemsave,
Uibarbuttonsystemitemadd,
Uibarbuttonsystemitemflexiblespace,
Uibarbuttonsystemitemfixedspace,
Uibarbuttonsystemitemcompose,
Uibarbuttonsystemitemreply,
Uibarbuttonsystemitemaction,
Uibarbuttonsystemitemorganize,
Uibarbuttonsystemitembookmarks,
Uibarbuttonsystemitemsearch,
Uibarbuttonsystemitemrefresh,
Uibarbuttonsystemitemstop,
Uibarbuttonsystemitemcamera,
Uibarbuttonsystemitemtrash,
Uibarbuttonsystemitemplay,
Uibarbuttonsystemitempause,
Uibarbuttonsystemitemrewind,
Uibarbuttonsystemitemfastforward,
#if __iphone_3_0 <= __iphone_os_version_max_allowed
Uibarbuttonsystemitemundo,
Uibarbuttonsystemitemredo,
#endif
#if __iphone_4_0 <= __iphone_os_version_max_allowed
Uibarbuttonsystemitempagecurl,
#endif
};
Finally, don't forget to implement right: method:
Copy Code code as follows:
-(void) Right: (ID) sender
{
NSLog (@ "Rightbarbuttonitem");
}
Uibarbuttonitem of custom text
Self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] initwithtitle:@ "Back" style: Uibarbuttonitemstyleplain target:self Action: @selector (back:)];
Uibarbuttonitemstyle has the following three options:
Copy Code code as follows:
typedef ns_enum (Nsinteger, Uibarbuttonitemstyle) {
Uibarbuttonitemstyleplain,
Uibarbuttonitemstylebordered Ns_enum_deprecated_ios (2_0, 8_0, "use Uibarbuttonitemstyleplain when minimum deployment Target is iOS7 or later "),
Uibarbuttonitemstyledone,
};
Implement back: Method:
Copy Code code as follows:
-(void) back: (ID) sender
{
[Self.navigationcontroller Popviewcontrolleranimated:yes];
}
Uibarbuttonitem of custom photos
Copy Code code as follows:
Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] initwithimage:[uiimage imagenamed:@ "test"] style: Uibarbuttonitemstyleplain target:self Action: @selector (right:)];
Customizing the UIView uibarbuttonitem
Customize the UIView, and then create the Uibarbuttonitem through the Initwithcustomview: method.
Copy Code code as follows:
UIView *testview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 40, 60)];
Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] initwithcustomview:testview];
See a friend in the background to ask questions:
I now need to change that navigation original return picture, also want to change back to text, how should change it, ask advice.
In fact, this can be used Initwithcustomview: To solve, custom uiview you can put Uiimageview and Uilabel. You can customize UIView, so what you want to define is OK.
Here's an interesting example:
first say the need:
1. Do a rightbarbuttonitem constantly rotating demo;
2. Click the Rightbarbuttonitem button to rotate or pause;
Final results show:
Is the rotation of the note graph.
Critical Code Presentation (commented):
Copy Code code as follows:
//
Viewcontroller.m
Navigationbtn
//
#import "ViewController.h"
#define Degrees_to_radians (angle) (angle)/180.0 * M_PI)
ImageView Rotation State Enumeration
typedef enum {
Rotatestatestop,
Rotatestaterunning,
}rotatestate;
@interface Viewcontroller ()
{
Rotation angle
CGFloat Imageviewangle;
Rotating ImageView
Uiimageview *imageview;
Rotation state
Rotatestate rotatestate;
}
@end
Copy Code code as follows:
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
self.title=@ "micro-letter public account: le coding";
[Self buildbarbuttonitem];
}
#pragma Mark adds Rightbarbuttonitem
-(void) buildbarbuttonitem{
ImageView = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@ "icon"]];
Imageview.autoresizingmask = Uiviewautoresizingnone;
Imageview.contentmode = Uiviewcontentmodescaletofill;
Imageview.bounds=cgrectmake (0, 0, 40, 40);
Set View as Circular
Imageview.layer.maskstobounds=yes;
IMAGEVIEW.LAYER.CORNERRADIUS=20.F;
UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];
Button.frame = CGRectMake (0, 0, 40, 40);
[Button Addsubview:imageview];
[Button addtarget:self action: @selector (animate) forcontrolevents:uicontroleventtouchupinside];
Imageview.center = Button.center;
Set Rightbarbuttonitem
Uibarbuttonitem *baritem = [[Uibarbuttonitem alloc] Initwithcustomview:button];
Self.navigationItem.rightBarButtonItem = Baritem;
}
#pragma mark clicks on Rightbarbuttonitem
-(void) Animate {
Change the ImageView rotation state
if (rotatestate==rotatestatestop) {
rotatestate=rotatestaterunning;
[Self rotateanimate];
}else{
Rotatestate=rotatestatestop;
}
}
#pragma mark Rotate Animation
-(void) rotateanimate{
imageviewangle+=50;
0.5 seconds to rotate 50 degrees
[UIView animatewithduration:0.5 delay:0.0 options:uiviewanimationoptioncurvelinear animations:^{
Imageview.transform = Cgaffinetransformmakerotation (Degrees_to_radians (Imageviewangle));
} completion:^ (BOOL finished) {
if (rotatestate==rotatestaterunning) {
[Self rotateanimate];
}
}];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
@end