IOS體系導航欄中有leftBarButtonItem和rightBarButtonItem,我們可以依據本身的需求來自界說這兩個UIBarButtonItem。
四種創立辦法
體系供給了四種創立的辦法:
- (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;
經由過程體系UIBarButtonSystemItem創立
自界說rightBarButtonItem,代碼以下:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];
UIBarButtonSystemItem有以下款式可以供選擇:
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
};
最初別忘了完成right:辦法:
- (void)right:(id)sender
{
NSLog(@"rightBarButtonItem");
}
自界說文字的UIBarButtonItem
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
UIBarButtonItemStyle有以下三種選擇:
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,
};
完成back:辦法:
- (void)back:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
自界說照片的UIBarButtonItem
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"test"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
自界說UIView的UIBarButtonItem
自界說UIView,然後經由過程initWithCustomView:辦法來創立UIBarButtonItem。
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 60)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];
看到有同伙在後台發問:
我如今即須要改誰人導航原生的前往圖片,也要改前往文字,應當怎樣改呢,求指教。
其實,這個便可以用initWithCustomView:來處理,自界說UIView你可以放UIImageView和UILabel。可以自界說UIView,那末想怎樣界說都是可以的。
上面來看一個風趣的例子:
先說一下需求:
1.做一個RightBarButtonItem赓續扭轉的Demo;
2.點擊RightBarButtonItem 按鈕扭轉或暫停;
終究後果展現:
就是誰人音符圖形的扭轉。
症結代碼展現(已加正文):
//
// ViewController.m
// NavigationBtn
//
#import "ViewController.h"
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
///ImageView扭轉狀況列舉
typedef enum {
RotateStateStop,
RotateStateRunning,
}RotateState;
@interface ViewController ()
{
///扭轉角度
CGFloat imageviewAngle;
///扭轉ImageView
UIImageView *imageView;
///扭轉狀況
RotateState rotateState;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"微信"賬號:樂Coding";
[self buildBarButtonItem];
}
#pragma mark 添加 RightBarButtonItem
-(void)buildBarButtonItem{
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
imageView.autoresizingMask = UIViewAutoresizingNone;
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.bounds=CGRectMake(0, 0, 40, 40);
//設置視圖為圓形
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;
//設置RightBarButtonItem
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barItem;
}
#pragma mark 點擊 RightBarButtonItem
- (void)animate {
//轉變ImageView扭轉狀況
if (rotateState==RotateStateStop) {
rotateState=RotateStateRunning;
[self rotateAnimate];
}else{
rotateState=RotateStateStop;
}
}
#pragma mark 扭轉動畫
-(void)rotateAnimate{
imageviewAngle+=50;
//0.5秒扭轉50度
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
imageView.transform = CGAff.netransformMakeRotation(DEGREES_TO_RADIANS(imageviewAngle));
} completion:^(BOOL finished) {
if (rotateState==RotateStateRunning) {
[self rotateAnimate];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
【詳解iOS運用中自界說UIBarButtonItem導航按鈕的創立辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!