一、工具欄控件:UIToolBar:UIView
介紹:
ToolBar工具欄是視圖View的屬性,可以在工具欄上添加工具欄按鈕Bar Button Item(可以是自定義的Custom、也可以是系統自帶的BarButtonSystemItem ),視圖控制器可以通過工具欄項對視圖中内容進行操作。
注意事項:
在導航欄控制器中會有一個UIToolBar執行個體,但預設是隐藏的,如果需要顯示,需要通過這個方法将其打開:

在這裡需要注意的是,與UINavigationBar類似,導航控制器擁有且隻擁有一個UIToolBar執行個體,但UIToolBar擁有的UIBarButtonItem執行個體,是由視圖控制器進行管理的,如下所示:
工具欄風格:
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0, //預設風格,藍色文字
UIBarStyleBlack = 1, //黑色背景,褐色文字
UIBarStyleBlackOpaque = 1, // 純黑色背景,白色文字
UIBarStyleBlackTranslucent = 2, // 透明黑色背景,白色文字
};
屬性:
@property(nonatomic) UIBarStyle barStyle; //工具欄風格,預設為藍色
@property(nonatomic,copy) NSArray *items; //工具欄中的按鈕單元,UIBarButtonItem
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent //是否透明
@property(nonatomic,retain) UIColor *tintColor; //按鈕顔色
@property(nonatomic,retain) UIColor *barTintColor; //工具欄顔色
方法:
※設定工具欄中的按鈕單元
- (void)setItems:(NSArray *)items animated:(BOOL)animated;
※設定工具欄的背景圖像
- (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
※擷取工具欄的背景圖像
- (UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
※設定工具欄的陰影圖像
- (void)setShadowImage:(UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;
※擷取工具欄的陰影圖像
- (UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom ;
二、工具欄按鈕控件:UIBarButtonItem:UIBarItem
@property(nonatomic) UIBarButtonItemStyle style; //風格
@property(nonatomic) CGFloat width; // 調節間距寬度
@property(nonatomic,copy) NSSet *possibleTitles; // 标題
@property(nonatomic,retain) UIView *customView; // 自定義視圖
@property(nonatomic) SEL action; // 事件
@property(nonatomic,assign) id target; // 目标代理
按鈕單元風格UIBarButtonItem:
typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) {
UIBarButtonItemStylePlain, //普通風格
UIBarButtonItemStyleBordered, //有邊界的風格
UIBarButtonItemStyleDone, //藍色風格
系統自帶的按鈕:
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,
主要方法:
※用圖像初始化
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
※圖檔(包括豎屏和橫屏顯示不同的圖檔)
- (instancetype)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone 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;
舉例舉例如下:
1.在工具欄中全部添加系統自帶的按鈕單元:
示範結果如下:
2.在工具欄上添加圖像按鈕
示範結果如下:
程式猿神奇的手,每時每刻,這雙手都在改變着世界的互動方式!
本文轉自當天真遇到現實部落格園部落格,原文連結:http://www.cnblogs.com/XYQ-208910/p/4850939.html,如需轉載請自行聯系原作者