Toolbar的使用

一、控制器.matom

- (void)initWithTool {
    //toolbar的背景顏色
    [self.trendsToolbar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    //自定義toolbar數據
    NSMutableArray *multArray = [NSMutableArray arrayWithCapacity:1];
    _titleArray = @[QHLocalizedString(@"動態", nil), QHLocalizedString(@"關注", nil), QHLocalizedString(@"FM", nil), QHLocalizedString(@"個人", nil)];
    _imageArray = @[@"Vector_Smart_Hot_OFF", @"Vector_Smart_Attention_OFF", @"Vector_Smart_FM_OFF", @"Vector_Smart_My_OFF"];
    _seleImageArray = @[@"Vector_Smart_Hot_ON", @"Vector_Smart_Attention_ON", @"Vector_Smart_FM_ON", @"Vector_Smart_My_ON"];
    _models = [NSMutableArray arrayWithCapacity:1];
    
    //model
    for (int i = 0; i<_titleArray.count; i++) {
        QHPiHeroClassModel *model = [[QHPiHeroClassModel alloc] init];
        model.imageString = _imageArray[i];
        model.title = _titleArray[i];
        model.seleImageString = _seleImageArray[i];
        
        [_models addObject:model];
    }
    //繪製UIView
    for (int i = 0; i<_titleArray.count; i++) {
        UIView *base = [[UIView alloc] initWithFrame:CGRectMake(Main_Screen_Width/5*i, 0, Main_Screen_Width/5, 44)];
         QHPiTendenciesToolView *tool = [[NSBundle mainBundle]loadNibNamed:@"QHPiTendenciesToolView" owner:nil options:nil].lastObject;
        [base addSubview:tool];
        
        tool.norTag = i;
        tool.model = _models[i];
        tool.isSelect = i==0?YES:NO;
        
        [tool mas_makeConstraints:^(MASConstraintMaker *make) {
           make.edges.equalTo(base).insets(UIEdgeInsetsZero);
        }];
        //回調
        __weak typeof(self) weakSelf = self;
        [tool toolbarClickedWithTag:^(NSInteger tag) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            [strongSelf bottomToolClicked:tag];
        }];
        //添加UIBarButtonItem
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:base];
        [multArray addObject:barButtonItem];
        
    }
    //最開始點擊置0
    _preSelect = 0;
   
    [self.trendsToolbar setItems:multArray animated:YES];
}

#pragma mark 回調方法
- (void)bottomToolClicked:(NSInteger)tag {

    if (_preSelect == tag) {
        return;
    }
    //以前的點擊
    UIBarButtonItem *preItem = _trendsToolbar.items[_preSelect];
    QHPiTendenciesToolView *toolView = [preItem.customView subviews][0];
    toolView.isSelect = NO;
    
    //當前的點擊
    UIBarButtonItem *cItem = _trendsToolbar.items[tag];
    QHPiTendenciesToolView *cToolView = [cItem.customView subviews][0];
    cToolView.isSelect = YES;
    
    _preSelect = tag;
}

二、QHPiTendenciesToolView.mcode

- (void)setModel:(QHPiHeroClassModel *)model {
    _model = model;
    self.titleLabel.text = model.title;
    
}

- (void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    
    if (_isSelect) {
        self.treImageView.image = [IMAGENAMED(_model.seleImageString) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.titleLabel.textColor = UIColorFromRGB(0x49aeef);
    }else {
        self.treImageView.image = [IMAGENAMED(_model.imageString) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.titleLabel.textColor = UIColorFromRGB(0x969699);
    }
}

- (void)toolbarClickedWithTag:(void (^)(NSInteger))toolBlock {
    _toolBlock = toolBlock;
    
}

- (IBAction)didSelectWithTag:(id)sender {
    [self setIsSelect:YES];

    if (_toolBlock) {
        _toolBlock(_norTag);
    }
    
}

三、QHPiHeroClassModel.mci

#import <Foundation/Foundation.h>

@interface QHPiHeroClassModel : NSObject

@property (nonatomic, strong) NSString *imageString;
@property (nonatomic, strong) NSString *seleImageString;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *desc;
@property (nonatomic, assign) NSUInteger count;

@end
相關文章
相關標籤/搜索