1.inputAccessoryView
UITextField和UITextView的這個屬性是readwrite的,可以自定義一個view設置為UITextField的inputAccessoryView
UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
[button setTitle:@"完成"forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[toolView addSubview:button];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 7,50, 30)];
[button1 setTitle:@"取消"forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[toolView addSubview:button1];
_textL.inputAccessoryView = toolView;
效果:
01.jpeg
當然可以直接將UIView替換成UIToolBar,自帶背景色和border:
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
效果:
2.jpeg
不足之處:每次都要為UITextField或UITextView設置inputAccessoryView
改善:創建繼承自UITextField或UITextView的類,在.m的init方法(或者drawRect:方法)統一設置:
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,44)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7,50, 30)];
[button setTitle:@"完成"forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[bar addSubview:button];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(30, 7,50, 30)];
[button1 setTitle:@"取消"forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[bar addSubview:button1];
self.inputAccessoryView = bar;
}
自定義TextField需要將按鈕的事件傳回到控制器執行(block/代理/通知 等方式)
2.監聽鍵盤彈出和收回
添加通知:
[self.view addSubview:self.editView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
收到通知之后可以獲取鍵盤的一系列信息:
{
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 775}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}";
UIKeyboardIsLocalUserInfoKey = 1;
}
其中UIKeyboardFrameEndUserInfoKey是鍵盤彈出或收起結束之后的frame
處理:
- (void)keyboardWillShow:(NSNotification *)noti {
NSDictionary *userInfo = noti.userInfo;
CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat y = endRect.origin.y;
[self.editView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.height.mas_equalTo(44);
make.top.mas_equalTo(y - 44);
}];
}
- (void)keyboardWillHide:(NSNotification *)noti {
NSDictionary *userInfo = noti.userInfo;
CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat y = endRect.origin.y;
[self.editView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.height.mas_equalTo(44);
make.top.mas_equalTo(y - 44);
}];
}
當然不要忘了移除通知