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<items.count; i++) {
        id item = [items objectAtIndex:i];
        if ([item isKindOfClass:[UIBarButtonItem class]])
        {
            [itemsAry addObject:(UIBarButtonItem*)item];
        }
        else if ([item isKindOfClass:[NSString class]])
        {
            //如果不是图片就作为文字显示
            UIColor *color = [UIColor whiteColor];
            CGFloat titleSize = 20;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            NSString *dataStr = [items objectAtIndex:i];
            UIImage *image = [UIImage imageNamed:dataStr];
            if (image)
            {
                [btn setImage:image forState:UIControlStateNormal];
            }
            else
            {
                btn = [UIButton buttonWithType:UIButtonTypeSystem];
                [btn setTitle:dataStr forState:UIControlStateNormal];
                [btn setTitleColor:color forState:UIControlStateNormal];
                btn.titleLabel.font = [UIFont boldSystemFontOfSize:titleSize];
            }
            y++;
            btn.frame = CGRectMake(0,0, size.width, size.height);
            btn.tag = tag+y;
            btn.autoresizingMask = UIViewAnimationTransitionNone;
            //    setBtn.backgroundColor = [UIColor blueColor];
            [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            UIBarButtonItem* btnItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
            [itemsAry addObject:btnItem];
        }
    }
    [self setItems:itemsAry];
}

#pragma mark - 回调下标
-(void)buttonClick:(UIButton*)button
{
    if (button.tag>=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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值