iOS 为键盘添加隐藏按钮

 

 

 

// 为键盘添加隐藏按钮
    UIToolbar * backView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [backView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"隐藏键盘" forState:UIControlStateNormal];
    btn.frame = CGRectMake(2, 5, 70, 25);
    [btn addTarget:self action:@selector(cancelInput:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
    NSArray * buttonsArray = @[btnSpace,doneBtn];
    [backView setItems:buttonsArray];
    [self.searchBar setInputAccessoryView:backView];

 

posted @ 2016-07-16 02:18  Ficow  阅读(663)  评论(0编辑  收藏  举报