UIToolBar (API+自定义工具栏)

初始化

navToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, k_navBar_height)];
    navToolBar.barStyle = UIBarStyleBlackTranslucent;
//    [navToolBar setBackgroundImage:[UIImage imageNamed:@"transparentImage"] forToolbarPosition:0 barMetrics:0];
    [self.view addSubview:navToolBar];
    [navToolBar setBackgroundImage:[UIImage new]
                forToolbarPosition:UIBarPositionAny
                        barMetrics:UIBarMetricsDefault];
    [navToolBar setShadowImage:[UIImage new]
            forToolbarPosition:UIToolbarPositionAny];
    [navToolBar setBackgroundColor:[UIColor colorWithWhite:.06f alpha:.3f]];
    
    UIButton *imageBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [imageBtn setFrame:CGRectMake(0,0,50,k_navBar_height)];
    [imageBtn setBackgroundImage:[UIImage imageNamed:@"lookForPhoto_icon"] forState:UIControlStateNormal];
    [imageBtn addTarget:self action:@selector(selectedEditImg) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *imageItem=[[UIBarButtonItem alloc] initWithCustomView:imageBtn];
    
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(0,0,45,k_navBar_height)];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"swich"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(cameraToggle) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    UIButton *lightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [lightBtn setFrame:CGRectMake(0,0,50,k_navBar_height)];
    [lightBtn setBackgroundImage:[UIImage imageNamed:@"lighting_close"] forState:UIControlStateNormal];
    [lightBtn setBackgroundImage:[UIImage imageNamed:@"lighting"] forState:UIControlStateSelected];
    [lightBtn addTarget:self action:@selector(changeFlash:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *lightItem=[[UIBarButtonItem alloc] initWithCustomView:lightBtn];
    lightBtn.tag = k_item_tag+1;
    
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    [navToolBar setItems:[NSArray arrayWithObjects:flexSpace,flexSpace,lightItem,flexSpace,rightItem,flexSpace,imageItem,nil]];



自定义

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    //底部栏
    CGSize toolBarSize = CGSizeMake(k_item_width, k_item_width);
    toolBar = [[Toolbar alloc]init];
    [self tranFromToolBarFrame:toolBar height:k_toolBar_height];
    toolBar.autoresizesSubviews = NO;
    toolBar.autoresizingMask = UIViewAnimationTransitionNone;
    [self.view bringSubviewToFront:toolBar];
    [self.view addSubview:toolBar];
    toolBar.backgroundFace = [UIColor blackColor];
[toolBar toolbarSetItems:[NSArray arrayWithObjects:flexSpace,@"share_icon",flexSpace,@"3d_icon",flexSpace,@"edit_icon",flexSpace,@"delete_icon",flexSpace,nil] size:toolBarSize tag:k_item_tag];

#import "Toolbar.h"
@interface Toolbar(){
    
    NSUInteger item_tag;
}
@end

@implementation Toolbar

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self==[super initWithFrame:frame])
    {
        [self setBackgroundImage:[UIImage new]
              forToolbarPosition:UIBarPositionAny
                      barMetrics:UIBarMetricsDefault];
        [self setShadowImage:[UIImage new]
          forToolbarPosition:UIToolbarPositionAny];
        [self setBackgroundColor:[UIColor colorWithWhite:.06f alpha:.3f]];
    }
    return self;
}

-(void)setBackgroundFace:(id)backgroundFace
{
    if ([backgroundFace isKindOfClass:[UIColor class]])
    {
        [self setBackgroundColor:backgroundFace];

    }
    else if ([backgroundFace isKindOfClass:[UIImage class]])
    {
        [self setBackgroundImage:backgroundFace
              forToolbarPosition:UIBarPositionAny
                      barMetrics:UIBarMetricsDefault];
    }
    else
    {
        //Default lightGray
        [self setBackgroundColor:[UIColor colorWithWhite:.06f alpha:.3f]];
    }
}


-(void)toolbarSetItems:(NSArray *)items size:(CGSize)size tag:(NSUInteger)tag
{
    item_tag = tag;
    NSUInteger y = 0;
    NSMutableArray *itemsAry = [NSMutableArray array];
    for (NSUInteger i=0; i=7000&&button.tag<8000)
    {
        if (button.tag ==index)
        {
            return;
        }
    }
    
    index = button.tag;
    
    NSInteger tag = button.tag-item_tag;
    if (self.block)
    {
        self.block(tag,button);
    }
    
}


@interface Toolbar : UIToolbar{
    NSUInteger index;
}

@property(nonatomic,copy) ToolbarBlock block;
//imageName、[color ..: alpha:..]
@property(nonatomic,assign) id backgroundFace;
/**
 *  数组中添加UIBarButtonItem clall 和 UIButton 的图片
 *
 *  @param items UIBarButtonItem & imageString
 */
-(void)toolbarSetItems:(NSArray *)items size:(CGSize)size tag:(NSUInteger)tag;

@end




你可能感兴趣的:(IOS_UIKit库)