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];//在视图控制器消除时,移除键盘事件的通知
}
相关文章
|
6月前
|
JavaScript 前端开发 Java
获取键盘事件的keyCode属性
获取键盘事件的keyCode属性
|
iOS开发
UISearchBar去除背景
UISearchBar去除背景
158 0
UISearchBar去除背景
|
iOS开发
IOS——UITextField自动适应键盘弹出
IOS——UITextField自动适应键盘弹出
196 0
SwiftUI—通过Button打开一个模态窗口
SwiftUI—通过Button打开一个模态窗口
352 0
SwiftUI—通过Button打开一个模态窗口
UITextField 自定义使用
UITextField自定义使用(一)UITextField自定义使用(二)
532 0
1112. Stucked Keyboard (20)
#include #include #include using namespace std; int main() { int k; string s; map ma; map mb; cin >> k >> s; for(int i = 1; i < s.
874 0