textField.inputAccessoryView = [self addToolbar];
- (UIToolbar*)addToolbar{
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.maskView.frame), 40)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(numberFieldCancle)];
toolbar.items=@[space,bar];
return toolbar;
}
- (void)numberFieldCancle{
[textField resignFirstResponder]; //收起键盘
}
/**设置默认的附加视图
*@paramtarget 方法执行者
*@paramaction 方法
*/
- (void)setDefaultInputAccessoryViewWithTarget:(id) target action:(SEL) action
{
UIToolbar*toolBar = [[UIToolbaralloc]init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:target action:action];
doneBtn.tintColor=AppMainColor;
NSArray*items =@[flexSpace,doneBtn];
toolBar.items= items;
[toolBarsizeToFit];
self.inputAccessoryView = toolBar;
}
//使用
[textField setDefaultInputAccessoryViewWithTarget:self action:@selector(numberFieldCancle)];
网友评论