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

首頁 > 系統 > iOS > 正文

詳解iOS應用中自定義UIBarButtonItem導航按鈕的創建方法

2020-02-19 15:48:24
字體:
來源:轉載
供稿:網友

導航可以很容易地幫助我們管理視圖控制器,導航的重要性不言而喻,這是每個iOS初學者都應該面對的一個問題,今天武林技術頻道帶大家學習詳解iOS應用中自定義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 按鈕旋轉或暫停;
最終效果展示:

201642692022584.jpg (407×252)

201642692104322.jpg (408×222)

就是那個音符圖形的旋轉。
關鍵代碼展示(已加注釋):

?

//
// 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 = 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應用中自定義UIBarButtonItem導航按鈕的創建方法之后,相信各位朋友們,都已經搞清楚了這個問題了吧。

?

?

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贺州市| 嫩江县| 贺兰县| 陆河县| 华容县| 平阳县| 东乌| 霍城县| 湖州市| 安吉县| 神木县| 梁河县| 县级市| 海安县| 潼关县| 炎陵县| 福鼎市| 长治县| 南充市| 本溪市| 衢州市| 石城县| 甘洛县| 新巴尔虎左旗| 兴隆县| 凉山| 洱源县| 龙口市| 内乡县| 黑山县| 子长县| 永丰县| 花莲市| 永修县| 子洲县| 朔州市| 平原县| 张家界市| 梁平县| 华亭县| 类乌齐县|