詳解iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法
iOS系統(tǒng)導(dǎo)航欄中有l(wèi)eftBarButtonItem和rightBarButtonItem,我們可以根據(jù)自己的需求來自定義這兩個(gè)UIBarButtonItem。
四種創(chuàng)建方法
系統(tǒng)提供了四種創(chuàng)建的方法:
- (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;
通過系統(tǒng)UIBarButtonSystemItem創(chuàng)建
自定義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
};
最后別忘了實(shí)現(xiàn)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,
};
實(shí)現(xiàn)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:方法來創(chuàng)建UIBarButtonItem。
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 60)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];
看到有朋友在后臺(tái)提問:
我現(xiàn)在即需要改那個(gè)導(dǎo)航原生的返回圖片,也要改返回文字,應(yīng)該怎么改呢,求指教。
其實(shí),這個(gè)就可以用initWithCustomView:來解決,自定義UIView你可以放UIImageView和UILabel??梢宰远xUIView,那么想怎么定義都是可以的。
下面來看一個(gè)有趣的例子:
先說一下需求:
1.做一個(gè)RightBarButtonItem不斷旋轉(zhuǎn)的Demo;
2.點(diǎn)擊RightBarButtonItem 按鈕旋轉(zhuǎn)或暫停;
最終效果展示:


就是那個(gè)音符圖形的旋轉(zhuǎn)。
關(guān)鍵代碼展示(已加注釋):
//
// ViewController.m
// NavigationBtn
//
#import "ViewController.h"
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
///ImageView旋轉(zhuǎn)狀態(tài)枚舉
typedef enum {
RotateStateStop,
RotateStateRunning,
}RotateState;
@interface ViewController ()
{
///旋轉(zhuǎn)角度
CGFloat imageviewAngle;
///旋轉(zhuǎn)ImageView
UIImageView *imageView;
///旋轉(zhuǎn)狀態(tài)
RotateState rotateState;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"微信公眾賬號(hào):樂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);
//設(shè)置視圖為圓形
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;
//設(shè)置RightBarButtonItem
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barItem;
}
#pragma mark 點(diǎn)擊 RightBarButtonItem
- (void)animate {
//改變ImageView旋轉(zhuǎn)狀態(tài)
if (rotateState==RotateStateStop) {
rotateState=RotateStateRunning;
[self rotateAnimate];
}else{
rotateState=RotateStateStop;
}
}
#pragma mark 旋轉(zhuǎn)動(dòng)畫
-(void)rotateAnimate{
imageviewAngle+=50;
//0.5秒旋轉(zhuǎn)50度
[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 resources that can be recreated.
}
@end
- 詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件
- 關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法
- IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例
- iOS開發(fā)中UISwitch按鈕的使用方法簡(jiǎn)介
- 詳解iOS-按鈕單選與多選邏輯處理
- iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
- iOS 防止按鈕多次點(diǎn)擊造成多次響應(yīng)的方法
- iOS實(shí)現(xiàn)全局懸浮按鈕
相關(guān)文章
iOS實(shí)現(xiàn)簡(jiǎn)易抽屜效果、雙邊抽屜效果
這篇文章主要為大家詳細(xì)介紹了兩款iOS抽屜效果,簡(jiǎn)易抽屜效果、以及雙邊抽屜效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
IOS初始化控制器的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了IOS初始化控制器的實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,這里提供兩種實(shí)現(xiàn)方法分別是ViewControllViewController方法和 ViewControllViewController 與 xib方法,需要的朋友可以參考下2017-10-10
iOS開發(fā)中WebView的基本使用方法簡(jiǎn)介
這篇文章主要介紹了iOS開發(fā)中WebView的基本使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11
IOS實(shí)現(xiàn)視頻動(dòng)畫效果的啟動(dòng)圖
這篇文章實(shí)現(xiàn)的是一個(gè)關(guān)于啟動(dòng)頁或者引導(dǎo)頁的視頻動(dòng)畫效果的實(shí)現(xiàn)過程,對(duì)于大家開發(fā)APP具有一定的參考借鑒價(jià)值,有需要的可以來看看。2016-09-09
IOS數(shù)字鍵盤左下角添加完成按鈕的實(shí)現(xiàn)方法
這篇文章主要介紹了IOS數(shù)字鍵盤左下角添加完成按鈕的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能實(shí)現(xiàn)類似這樣的功能,需要的朋友可以參考下2017-08-08
Unity3d發(fā)布IOS9應(yīng)用時(shí)出現(xiàn)中文亂碼的解決方法
這里給大家分享的是使用UNity3d發(fā)布IOS9應(yīng)用的時(shí)候,遇到出現(xiàn)中文亂碼的現(xiàn)象的解決方法,核心內(nèi)容非常簡(jiǎn)單就是批量修改NGUI的label字體,下面把代碼奉上。2015-10-10

