最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年04月26日 09:22:57   作者:李剛  
這篇文章主要介紹了iOS應用中自定義UIBarButtonItem導航按鈕的創(chuàng)建方法,文中舉了一個自定義圖片的UIBarButtonItem實例,比較具有代表性,需要的朋友可以參考下

iOS系統(tǒng)導航欄中有l(wèi)eftBarButtonItem和rightBarButtonItem,我們可以根據(jù)自己的需求來自定義這兩個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
};

最后別忘了實現(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,
};

實現(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];

看到有朋友在后臺提問:

我現(xiàn)在即需要改那個導航原生的返回圖片,也要改返回文字,應該怎么改呢,求指教。
其實,這個就可以用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旋轉狀態(tài)枚舉
typedef enum {
RotateStateStop,
RotateStateRunning,
}RotateState;

@interface ViewController ()
{
///旋轉角度
CGFloat imageviewAngle;
///旋轉ImageView
UIImageView *imageView;
///旋轉狀態(tài)
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旋轉狀態(tài)
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自定義UITabBar仿今日頭條效果

    iOS自定義UITabBar仿今日頭條效果

    這篇文章主要為大家詳細介紹了iOS自定義UITabBar仿今日頭條效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • iOS實現(xiàn)簡易抽屜效果、雙邊抽屜效果

    iOS實現(xiàn)簡易抽屜效果、雙邊抽屜效果

    這篇文章主要為大家詳細介紹了兩款iOS抽屜效果,簡易抽屜效果、以及雙邊抽屜效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 詳解iOS本地推送與遠程推送

    詳解iOS本地推送與遠程推送

    這篇文章主要為大家詳細介紹了iOS本地推送與遠程推送,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • IOS初始化控制器的實現(xiàn)方法總結

    IOS初始化控制器的實現(xiàn)方法總結

    這篇文章主要介紹了IOS初始化控制器的實現(xiàn)方法總結的相關資料,這里提供兩種實現(xiàn)方法分別是ViewControllViewController方法和 ViewControllViewController 與 xib方法,需要的朋友可以參考下
    2017-10-10
  • iOS開發(fā)中WebView的基本使用方法簡介

    iOS開發(fā)中WebView的基本使用方法簡介

    這篇文章主要介紹了iOS開發(fā)中WebView的基本使用方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • ios仿側邊抽屜效果實現(xiàn)代碼

    ios仿側邊抽屜效果實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了ios仿側邊抽屜效果實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • IOS實現(xiàn)視頻動畫效果的啟動圖

    IOS實現(xiàn)視頻動畫效果的啟動圖

    這篇文章實現(xiàn)的是一個關于啟動頁或者引導頁的視頻動畫效果的實現(xiàn)過程,對于大家開發(fā)APP具有一定的參考借鑒價值,有需要的可以來看看。
    2016-09-09
  • ios觸屏事件指南

    ios觸屏事件指南

    這篇文章主要給大家總結介紹了2種實現(xiàn)ios觸屏事件的方法,簡單實用,有需要的小伙伴可以參考下。
    2015-05-05
  • IOS數(shù)字鍵盤左下角添加完成按鈕的實現(xiàn)方法

    IOS數(shù)字鍵盤左下角添加完成按鈕的實現(xiàn)方法

    這篇文章主要介紹了IOS數(shù)字鍵盤左下角添加完成按鈕的實現(xiàn)方法的相關資料,希望通過本文能實現(xiàn)類似這樣的功能,需要的朋友可以參考下
    2017-08-08
  • Unity3d發(fā)布IOS9應用時出現(xiàn)中文亂碼的解決方法

    Unity3d發(fā)布IOS9應用時出現(xiàn)中文亂碼的解決方法

    這里給大家分享的是使用UNity3d發(fā)布IOS9應用的時候,遇到出現(xiàn)中文亂碼的現(xiàn)象的解決方法,核心內容非常簡單就是批量修改NGUI的label字體,下面把代碼奉上。
    2015-10-10

最新評論

东港市| 班玛县| 大竹县| 巴南区| 民县| 辉县市| 洛南县| 青龙| 融水| 桑日县| 美姑县| 蓬安县| 田东县| 五大连池市| 江北区| 平阴县| 泾阳县| 钦州市| 会昌县| 景东| 武安市| 娄烦县| 林芝县| 潢川县| 巴林左旗| 贡嘎县| 舞钢市| 鄂托克旗| 新兴县| 海林市| 汉阴县| 佳木斯市| 出国| 利津县| 疏附县| 霞浦县| 华池县| 三明市| 清镇市| 通辽市| 博罗县|