国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > iOS > 正文

詳解iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法

2019-10-21 18:54:26
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這篇文章主要介紹了iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法,文中舉了一個(gè)自定義圖片的UIBarButtonItem實(shí)例,比較具有代表性,需要的朋友可以參考下
 

iOS系統(tǒng)導(dǎo)航欄中有l(wèi)eftBarButtonItem和rightBarButtonItem,我們可以根據(jù)自己的需求來(lái)自定義這兩個(gè)UIBarButtonItem。

四種創(chuàng)建方法

系統(tǒng)提供了四種創(chuàng)建的方法:

復(fù)制代碼代碼如下:

- (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;


通過(guò)系統(tǒng)UIBarButtonSystemItem創(chuàng)建

 

自定義rightBarButtonItem,代碼如下:

復(fù)制代碼代碼如下:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(right:)];

UIBarButtonSystemItem有以下樣式可以供選擇:
復(fù)制代碼代碼如下:

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:方法:
復(fù)制代碼代碼如下:

- (void)right:(id)sender
{
    NSLog(@"rightBarButtonItem");
}

自定義文字的UIBarButtonItem

 

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
UIBarButtonItemStyle有以下三種選擇:

復(fù)制代碼代碼如下:

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:方法:
復(fù)制代碼代碼如下:

- (void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

自定義照片的UIBarButtonItem
復(fù)制代碼代碼如下:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"test"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];

自定義UIView的UIBarButtonItem

 

自定義UIView,然后通過(guò)initWithCustomView:方法來(lái)創(chuàng)建UIBarButtonItem。

復(fù)制代碼代碼如下:

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 60)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:testView];

看到有朋友在后臺(tái)提問(wèn):

 

我現(xiàn)在即需要改那個(gè)導(dǎo)航原生的返回圖片,也要改返回文字,應(yīng)該怎么改呢,求指教。
其實(shí),這個(gè)就可以用initWithCustomView:來(lái)解決,自定義UIView你可以放UIImageView和UILabel。可以自定義UIView,那么想怎么定義都是可以的。

下面來(lái)看一個(gè)有趣的例子:
先說(shuō)一下需求:
1.做一個(gè)RightBarButtonItem不斷旋轉(zhuǎn)的Demo;
2.點(diǎn)擊RightBarButtonItem 按鈕旋轉(zhuǎn)或暫停;
最終效果展示:

iOS應(yīng)用,UIBarButtonItem,導(dǎo)航按鈕

iOS應(yīng)用,UIBarButtonItem,導(dǎo)航按鈕

就是那個(gè)音符圖形的旋轉(zhuǎn)。
關(guān)鍵代碼展示(已加注釋?zhuān)?/p>

復(fù)制代碼代碼如下:

//
// 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


復(fù)制代碼代碼如下:

@implementation ViewController

 

- (void)viewDidLoad
{
[super viewDidLoad];
self.title=@"微信公眾賬號(hào):樂(lè)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)畫(huà)
-(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



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 德庆县| 新野县| 庆安县| 台安县| 内江市| 武安市| 滦南县| 高平市| 南平市| 盐亭县| 江都市| 汕头市| 比如县| 河池市| 乌苏市| 科技| 兴海县| 宁远县| 从江县| 滨海县| 玉田县| 双柏县| 九龙县| 新乐市| 辽宁省| 黄骅市| 汝州市| 宝兴县| 衡南县| 吉林市| 襄垣县| 平塘县| 霞浦县| 瓮安县| 六枝特区| 兴化市| 巧家县| 新疆| 六安市| 金平| 桃园县|