Why UIBarButtonItem not getting clicks in UIToolbar set as inputAccessoryView of UITextField?

六月ゝ 毕业季﹏ 提交于 2019-12-10 19:49:05

问题


UIBarItem does not respond to clicks inside UIToolbar which is setup as inputAccessoryView on a UITextField.

The button does not show click animation when I try to click it, callback does not get called. My setup looks like:

@interface MyViewController()
@property (weak, nonatomic) IBOutlet UITextField *closeDateTextField;
@property (strong, nonatomic) UIToolbar * datePickerToolbar;
@end

I setup toolbar with button:

- (void)viewDidLoad {

    self.datePickerToolbar = [[UIToolbar alloc] init];
    UIBarButtonItem * doneBtn =
        [[UIBarButtonItem alloc]
                initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                target:self
                action:@selector(hidePicker:)];

add button to toolbar and set toolbar as inputAccessoryView of UITextField:

    [self.datePickerToolbar setItems:@[doneBtn] animated:NO];
    self.closeDateTextField.inputAccessoryView = self.datePickerToolbar;
}

When I click on closeDateTextField the keyboard appears with a Done button in toolbar but the button does not respond to click, the hidePicker: does not get called.

- (void)hidePicker:(id)sender {
    [self.closeDateTextField resignFirstResponder];
}

Any idea what I'm doing wrong?


回答1:


I just tried the following code and it worked just fine. Tested on both iOS 6 & 7 simulator.

@interface HSViewController

@property (weak, nonatomic) IBOutlet UITextField *closeDateTextField;
@property (strong, nonatomic) UIToolbar * datePickerToolbar;

@end

@implementation HSViewController

- (void)viewDidLoad
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [super viewDidLoad];

    self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem * doneBtn =
    [[UIBarButtonItem alloc]
     initWithBarButtonSystemItem:UIBarButtonSystemItemDone
     target:self
     action:@selector(hidePicker:)];
    [self.datePickerToolbar setItems:@[doneBtn] animated:NO];

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
    textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:textField];
    self.closeDateTextField = textField;
    self.closeDateTextField.inputAccessoryView = self.datePickerToolbar;
}

- (void)hidePicker:(id)sender {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self.closeDateTextField resignFirstResponder];
}

@end



回答2:


self.datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 43)];

It worked with me when I did the toolbar height less than 44. 43 for an example

when it was 44 it crashes




回答3:


Instead of setting the UIToolbar frame you should only set its autoresizingMask:

self.datePickerToolbar.autoresizingMask = 
    UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

This way you'll have the expected behavior while being device/orientation independent.



来源:https://stackoverflow.com/questions/20046443/why-uibarbuttonitem-not-getting-clicks-in-uitoolbar-set-as-inputaccessoryview-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!