您的位置:首页 > 移动开发 > IOS开发

iOS_为数字键盘增加完成按钮即ToolBar

2016-12-13 12:21 477 查看
在输入价格的时候,要求弹出的键盘只能有数字和小数点。弹出的键盘没有完成键,想要退出键盘可以点击退出,但是为了更好的用户体验,在键盘上增加UIToolbar。



设置键盘样式:

self.textField.keyboardType = UIKeyboardTypeNumberPad;


设置ToolBar:

- (UIToolbar *)addToolbar {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 30)];
toolbar.tintColor = [UIColor blackColor];
toolbar.backgroundColor = [UIColor lightGrayColor];
UIBarButtonItem *prevItem = [[UIBarButtonItem alloc] initWithTitle:@"  <  " style:UIBarButtonItemStylePlain target:self action:@selector(prevTextField:)];
UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"  >  " style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField:)];
UIBarButtonItem *flbSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"完成",nil) style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
toolbar.items = @[prevItem,nextItem,flbSpace, doneItem];
return toolbar;
}


为textField添加ToolBar:

_textField.inputAccessoryView = [self addToolbar];


实现效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: