UITextField根据Keyboard自动移动

简介: UITextField根据Keyboard自动移动

1.注册通知

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 键盘高度变化通知,ios5.0新增的
#ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillChangeFrameNotification object:nil];
    }
#endif
}

2.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.bzView.frame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    self.bzView.frame = CGRectMake(0, self.view.bounds.size.height-44, 320, 44);
    return YES;
}
#pragma mark -键盘弹出时调用的方法
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification*)notification {
    NSDictionary *userInfo = [notification userInfo];
    // Get the origin of the keyboard when it's displayed.
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    self.bzView.frame = CGRectMake(0, self.view.bounds.size.height-keyboardRect.size.height-44, 320, 44);
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];//在视图控制器消除时,移除键盘事件的通知
}
相关文章
iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
372 0
iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
|
iOS开发
IOS——UITextField自动适应键盘弹出
IOS——UITextField自动适应键盘弹出
190 0
|
Swift
Swift: UITextView&UITextField 限制字数和输入内容
Swift: UITextView&UITextField 限制字数和输入内容
1370 0
Swift: UITextView&UITextField 限制字数和输入内容
UITextField 自定义使用
UITextField自定义使用(一)UITextField自定义使用(二)
515 0
|
数据安全/隐私保护