1. 程式人生 > >iOS 自定義鍵盤收回按鈕

iOS 自定義鍵盤收回按鈕

效果圖:這裡寫圖片描述
1.自定義一個TextField繼承自UITextField:
.h檔案

#import <UIKit/UIKit.h>

@interface AMPTextField : UITextField

@end

2.在.m檔案中重寫- (void)drawRect:(CGRect)rect {}方法

#import "AMPTextField.h"

@implementation AMPTextField


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0
,0, AMPScreenWidth,44)]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(AMPScreenWidth - 60, 7,50, 30)]; [button setTitle:@"完成"forState:UIControlStateNormal]; [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; button.layer.borderColor = [UIColor blueColor].CGColor
; button.layer.borderWidth =1; button.layer.cornerRadius =3; [bar addSubview:button]; self.inputAccessoryView = bar; [button addTarget:self action:@selector(print)forControlEvents:UIControlEventTouchUpInside]; } - (void) print { [self resignFirstResponder]; } @end

3.AMPTextField的鍵盤就會有完成按鈕,點選完成收回鍵盤。