目录
1、获取键盘的高度
2、创建键盘上的完成按钮
3、设置button按钮中的文字居中方式
4、自己绘制一张背景图
1、获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
self.keyboardEndHeight = keyboardRect.size.height;
self.keyboardEndHeight 自定义的
2、创建键盘上的完成按钮
- (UIToolbar *)createToolbar
{
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, KSCREENSIZE.width, 44)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonSystemItemDone target:self action:@selector(textFieldDone)];
toolBar.items = @[space, done];
return toolBar;
}
- (void)textFieldDone
{
[[self findFirstResponder] resignFirstResponder];
}
3、设置button按钮中的文字居中方式
UIToolbar *toolBar = [self createToolbar];
textField.inputAccessoryView = toolBar;
4、自己绘制一张背景图
// 不同情况的背景图 可以设置高亮状态下的颜色
+ (UIImage *)backgroundImageForButtonWithBounds:(CGRect)bounds normalColor:(UIColor*)normalColor hightedColor:(UIColor*)hightedColor isShenqing:(BOOL)shenqing
{//自绘了一张背景图
CGRect canvasRect = bounds;
CGRect paintingRect = CGRectMake(0, 0, canvasRect.size.width, canvasRect.size.height);;
UIGraphicsBeginImageContext(canvasRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
if (shenqing) {
CGContextSetFillColorWithColor(context, [normalColor CGColor]);
} else {
CGContextSetFillColorWithColor(context, [hightedColor CGColor]);
}
CGContextFillRect(context, paintingRect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}