美文网首页
iOS键盘上添加按钮

iOS键盘上添加按钮

作者: 流小星 | 来源:发表于2016-09-27 16:14 被阅读1214次

在键盘上添加按钮

代码如下

 - (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];
 }

相关文章

网友评论

      本文标题:iOS键盘上添加按钮

      本文链接:https://www.haomeiwen.com/subject/opncyttx.html