iOS: 工具欄控件UIToolBar和工具欄按鈕控件UIBarButtonItem的使用


一、工具欄控件: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.在工具欄中全部添加系統自帶的按鈕單元:

   //創建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];

演示結果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM