UIPickerView UIToolBar 按钮失效

今天在写一个 UIPickerView的时候,UIToolBar 上的 UIBarButtonItem的按钮(Cancel,Done)点击失效,但那个选择 PickerViewCell 中点击确实有效的。

UIPickerView UIToolBar 按钮失效_第1张图片
丑的 UIPickerView

大致代码是这样的

#pragma mark InitView
- (void)initSubViews {
    // UIPicker
    UIPickerView * pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [self.view addSubview:pickerView];
    [pickerView mas_makeConstraints:^(MASConstraintMaker *make){
        make.bottom.and.left.and.trailing.mas_equalTo(@0);
        
    }];
    //UIToolBar
    UIToolbar *toolBar = [[UIToolbar alloc] init];
    [toolBar setBarStyle:UIBarStyleBlack];
    // CancelItem、SpaceItem、DoneItem
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(toolBarCanelAction)];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(tooBarDoneAction)];
    toolBar.items = @[cancelItem,spaceItem,doneItem];
    [pickerView addSubview:toolBar];
    
    [toolBar mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.and.leading.and.trailing.mas_equalTo(@0);
    }];
}

#pragma mark UIPicker Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.dataArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"content === %@",self.dataArray[row]);
}

#pragma mark Private
- (void)toolBarCanelAction {
    NSLog(@"Cancel");
}
- (void)tooBarDoneAction {
    NSLog(@"Done");
}

#pragma mark Lazy
- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@"One",@"Two",@"Three",@"Four"];
    }
    return _dataArray;
}

为什么上述的两个点击事件没有效果呢?后来经过一个提醒可能确实是 pickerView 中的 subView 影响了 toolBar 上按钮的点击事件,改一下 toolBar 的父视图

 [self.view addSubview:toolBar];
 [toolBar mas_makeConstraints:^(MASConstraintMaker *make){
       make.leading.and.trailing.mas_equalTo(@0);
       make.bottom.equalTo(pickerView.mas_top);   
}];

ps: 改变 UIToolBar 主要的几个颜色

// 改变 toolBar 背景颜色
toolBar.barTintColor = [UIColor grayColor];
// 改变 toolBar 中所有 Item 颜色 
toolBar.tintColor = [UIColor yellowColor];
// 改变某个字体颜色
[cancelItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor  orangeColor]} forState:UIControlStateNormal];

另外我试过了在点击TextField 的时候是 OK 的,不涉及父视图这块的影响,只要如下稍微做处理就 OK 啦

textField.inputAccessoryView = toolBar;
textField.inputView = pickerView;
//Ps:当然此处,pickerView 是用 frame 创建的

你可能感兴趣的:(UIPickerView UIToolBar 按钮失效)