UIToolBar (API+自定义工具栏)

来源:互联网 发布:oracle转mysql powerde 编辑:程序博客网 时间:2024/05/16 02:35

初始化

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
原创粉丝点击