您的位置:首页 > 其它

view的frame高度根据键盘出现、消失改变

2016-07-14 17:05 495 查看
- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor
yellowColor];

    [[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(action:)
name:UIKeyboardWillShowNotification
object:nil];

    [[NSNotificationCenter
defaultCenter]addObserver:self
selector:@selector(action2:)
name:UIKeyboardWillHideNotification
object:nil];

}

//点击取消键盘

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent
*)event{

    [self.textfield
resignFirstResponder];

}

//键盘出现

- (void)action:(NSNotification *)notification{

    NSLog(@"%@",notification.userInfo);

    CGRect rect = [notification.userInfo[@"UIKeyboardFrameEndUserInfoKey"]CGRectValue];

    CGRect currentFrame =
self.view.frame;

    currentFrame.origin.y -= rect.size.height;

    self.view.frame = currentFrame;

}

//键盘消失

- (void)action2:(NSNotification *)notification{

    NSLog(@"%@",notification.userInfo);

    CGRect rect = [notification.userInfo[@"UIKeyboardFrameBeginUserInfoKey"]CGRectValue];

    CGRect currentFrame =
self.view.frame;

    currentFrame.origin.y += rect.size.height;

    self.view.frame = currentFrame;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: