简体   繁体   English

单击按钮未打开键盘

[英]Keyboard is not open on button click

I want to show keyboard when tap on button, but the case is that keyboard has accessory view and that is textfield. 我想在点击按钮时显示键盘,但是这种情况是键盘具有附件视图,也就是文本字段。 In short, I want to open keyboard with accessory view which is textfield. 简而言之,我想打开带有文本视图的附件视图的键盘。 Which is right way to do it..? 哪种方法正确?

I searched a lot, but none of those solutions helped me to achieve this. 我进行了很多搜索,但是这些解决方案都无法帮助我实现这一目标。 Please help me. 请帮我。

Here is my code : ViewController.m 这是我的代码: ViewController.m

@interface ViewController () <UITextFieldDelegate>
{
    UITextField *txtNote;
}

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *accView = [self createAccessoryView];
    [txtNote setInputAccessoryView:accView];
}

- (UIView *)createAccessoryView
{
    UIView *accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
    [accessoryView setBackgroundColor:[UIColor lightGrayColor]];
    [accessoryView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];

    CGFloat x = self.view.frame.size.width-65;

    txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)];
    txtNote.delegate = self;
    txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0];
    [accessoryView addSubview:txtNote];

    UIButton *btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSave.frame = CGRectMake(x, 5, 60, 35);
    btnSave.layer.cornerRadius = 5.0f;
    btnSave.clipsToBounds = TRUE;
    btnSave.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    [btnSave setTitle:@"Save" forState:UIControlStateNormal];
    [btnSave setBackgroundColor:[UIColor blackColor]];
    [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSave addTarget:self action:@selector(doneWithNumberPad:) forControlEvents:UIControlEventTouchUpInside];
    [btnSave setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
    [accessoryView addSubview:btnSave];

    return accessoryView;
}

- (IBAction)addNote:(UIButton *)button
{
    // On this click, I want to show keyboard.
    [txtNote becomeFirstResponder];
}

- (IBAction)doneWithNumberPad:(id)sender
{
    [txtNote resignFirstResponder];
}

Hope this helps you..I am getting the accessory view with keyboard on button click: 希望对您有帮助。.我正在单击键盘时得到附件视图:

  #import "ViewController.h"

   @interface ViewController ()<UITextFieldDelegate>{
      UITextField *txtNote;
      UIView *accessoryView ;
    }
  @end

 @implementation ViewController

 - (void)viewDidLoad
 {
   [super viewDidLoad];
   // call create accessoryview
   [self createAccessoryView];
 }

 - (UIView *)createAccessoryView
   {
     accessoryView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 200, 45)]; 
    [accessoryView setBackgroundColor:[UIColor lightGrayColor]];
    accessoryView.userInteractionEnabled = YES;

    //assign your dynamic frame values 
   txtNote = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, 100, 35)];
  txtNote.delegate = self;
  txtNote.backgroundColor = [UIColor greenColor];
  txtNote.userInteractionEnabled =YES;
  txtNote.keyboardAppearance = UIKeyboardAppearanceDefault;
  txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0];
  [accessoryView addSubview:txtNote];

  UIButton *btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
  btnSave.frame = CGRectMake(200, 5, 60, 35);
  btnSave.layer.cornerRadius = 5.0f;

  // btnSave.clipsToBounds = TRUE;

  btnSave.titleLabel.font = [UIFont systemFontOfSize:18.0f];
  [btnSave setTitle:@"Save" forState:UIControlStateNormal];
  [btnSave setBackgroundColor:[UIColor blackColor]];
  [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

  [btnSave addTarget:self action:@selector(doneWithNumberPad:) forControlEvents:UIControlEventTouchUpInside];
  [btnSave setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
  [accessoryView addSubview:btnSave];

  return accessoryView;
 }

  - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
   {
       NSLog(@"begin");
       return YES;
   }

//Call the accessory view with keyboard with button action
  -(IBAction)addBu:(id)sender{
     [txtNote becomeFirstResponder];
     [self.view addSubview:accessoryView];

    }

your 你的

CGFloat x = self.view.frame.size.width-65;

txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)];
txtNote.delegate = self;
txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0];
[accessoryView addSubview:txtNote];

should be 应该

CGFloat x = self.view.frame.size.width-65;

txtNote = [[UITextField alloc] initWithFrame:CGRectMake(0, 5, x-5, 35)];
txtNote.delegate = self;
txtNote.font = [UIFont systemFontOfSize:IS_IPAD?20.0:14.0];
[self.view addSubview:txtNote];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM