iOS键盘上添加按钮

在键盘上添加按钮

代码如下

 - (void)viewDidLoad { 
  [super viewDidLoad];
  textfield=[[UITextField alloc]initWithFrame:CGRectMake(10, 100, 320,    40)];
  textfield.backgroundColor=[UIColor redColor];
  [self.view addSubview:textfield];
  UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
  [topView setBarStyle:UIBarStyleBlackTranslucent];
  UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  btn.frame = CGRectMake(2, 5, 50, 25);
  [btn addTarget:self action:@selector(dismissKeyBoard) forControlEvents:UIControlEventTouchUpInside];
  [btn setTitle:@"完成"forState:UIControlStateNormal];

  UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
  NSArray * buttonsArray = [NSArray   arrayWithObjects:btnSpace,doneBtn,nil];
  [topView setItems:buttonsArray];
  [textfield setInputAccessoryView:topView];
}
  -(void)dismissKeyBoard {
      [textfield resignFirstResponder];
 }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容