一、工具欄控件:UIToolBar:UIView


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; // 目標代理
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.在工具欄中全部添加系統自帶的按鈕單元:
//創建toolBal導航欄實例 UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 667-60, 375, 40)]; //設置toolBar風格 toolbar.barStyle = UIBarStyleBlack; //將該控件添加到視圖中 [self.view addSubview:toolbar]; //創建控件上的按鈕單元 UIBarButtonItem *additem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; UIBarButtonItem *edititem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil]; UIBarButtonItem *titleitem = [[UIBarButtonItem alloc]initWithTitle:@"title" style:UIBarButtonItemStyleDone target:self action:nil]; //創建靈活調節按鈕單元,設置間隔 UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil]; //將按鈕單元都添加到數組中 NSArray *items = @[additem,flexibleitem,edititem,flexibleitem,titleitem]; //設置導航欄上的按鈕單元 [toolbar setItems:items animated:YES];
演示結果如下:
2.在工具欄上添加圖像按鈕
//創建toolBal導航欄實例 UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 30, 375, 40)]; //設置toolBar風格 toolbar.barStyle = UIBarStyleDefault; //將該控件添加到視圖中 [self.view addSubview:toolbar]; //創建控件上的按鈕單元 UIBarButtonItem *imageitem1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:nil]; UIBarButtonItem *imageitem2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"2.png"] style:UIBarButtonItemStylePlain target:self action:nil]; UIBarButtonItem *imageitem3 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"3.png"] style:UIBarButtonItemStylePlain target:self action:nil]; //創建靈活調節按鈕單元,設置間隔 UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:self action:nil]; //將按鈕單元都添加到數組中 NSArray *items = @[imageitem1,flexibleitem,imageitem2,flexibleitem,imageitem3]; //設置導航欄上的按鈕單元 [toolbar setItems:items animated:YES];
演示結果如下: