天天看點

iOS開發UINavigation系列二——UINavigationItem(二)

四、再看UIBarButtonItem

       上面我們了解到了,一個NavigationItem基本上是有三大部分組成的,目前顯示的部分,傳回按鈕部分,和ButtonItem部分,同樣對于建立和設定UIBarButoonItem,也有很多方法供我們使用。

       首先是建立與初始化的方法:

- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;

這個方法通過一個标題建立ButtonItem,其中style參數可以設定一個風格,枚舉如下:

typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {

   UIBarButtonItemStylePlain,

   UIBarButtonItemStyleDone,

};

這兩種風格差别并不大,如下是效果,Done風格的字型加粗一些:

iOS開發UINavigation系列二——UINavigationItem(二)

我們因為可以通過一個圖檔來建立BarButtonItem:

- (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;

- (instancetype)initWithImage:(nullable UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;

上面這兩個方法中,第一個方法與使用文字建立的方法類似,第二個方法多了一個landscapeImagePhone的參數,這個參數可以設定裝置橫屏時的圖檔。

我們也可以使用自定義的View來建立BarButtonItem:

- (instancetype)initWithCustomView:(UIView *)customView;

除了上面一些自定義的建立方法外,對于BarButtonItem這個對象,系統也封裝好了許多原生的可以供我們使用,建立的時候使用如下方法:

UIBarButtonItem * button = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];

iOS開發UINavigation系列二——UINavigationItem(二)

上面的SystemItem是系統為我們做好的許多buttonItem的類型,枚舉如下:

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,//顯示前進按鈕

   UIBarButtonSystemItemUndo,//顯示消除按鈕

   UIBarButtonSystemItemRedo ,//顯示重做按鈕

   UIBarButtonSystemItemPageCurl ,//在tool上有效

繼續閱讀