【问题标题】:Disable keyboard when禁用键盘时
【发布时间】:2023-03-23 11:59:02
【问题描述】:

当用户点击UITextField 时,我将显示UIDatePicker 用于输入,代码如下。但是当我这样做时,日期选择器和普通文本键盘会同时显示。我将如何禁用键盘显示?

- (IBAction)selectDateBegin:(id)sender {

if ([self.view viewWithTag:9]) {
    return;
}
CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
darkView.alpha = 0;
darkView.backgroundColor = [UIColor blackColor];
darkView.tag = 9;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] ;
[darkView addGestureRecognizer:tapGesture];
[self.view addSubview:darkView];

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
datePicker.tag = 10;
[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.tag = 11;
toolBar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
[toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
[self.view addSubview:toolBar];

[UIView beginAnimations:@"MoveIn" context:nil];
toolBar.frame = toolbarTargetFrame;
datePicker.frame = datePickerTargetFrame;
darkView.alpha = 0.5;
[UIView commitAnimations];}

【问题讨论】:

  • 请检查this链接以使用文本字段显示选择器视图

标签: ios objective-c ios6 uitextfield uistoryboard


【解决方案1】:
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
     [self.textFieldName resignFirstResponder];
   //
   //  Code ...continue,......for display DatePicker
   //
}

【讨论】:

    【解决方案2】:

    为您的日期选择器(导航栏具有确定和取消按钮)容器创建一个单独的 UIView,然后将其分配给 textfield.inputView 属性。更多详情请参考UITextField Class Reference

    这将加载您的日期选择器视图而不是默认键盘。

    你会在网上找到更多相同的例子。

    希望这是您正在寻找的。​​p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多