Whatsapp样式 - 键盘出现在工具栏下方

时间:2012-12-06 11:12:21

标签: ios keyboard toolbar whatsapp

如何使用whatsapp样式,消息空间在单击时会从底部向上推动键盘,同时也可以向上推动工具栏。然后当取消(即在后台点击)时,它会用工具栏将键盘向下推

1 个答案:

答案 0 :(得分:2)

这是通过U IKeyboardWillShowNotificationUIKeyboardDidShowNotificationUIKeyboardWillHideNotificationUIKeyboardDidHideNotification通知来完成的。 然后,当您处理通知时,您可以调整框架的高度:

例如:

- (void) keyboardWillShow:(NSNotification *)aNotification{

    NSDictionary* info = [aNotification userInfo];
    NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGFloat keyboardHeight = [aValue CGRectValue].size.height;

    self.keyboardVissible = YES;

    [UIView animateWithDuration:duration animations:^{
        CGRect frame = self.contentView.frame;
        frame.size.height -= keyboardHeight;
        self.contentView.frame = frame;
    }];
}

您需要注册才能收到通知,您应该只在视图可见时收听键盘通知,如果您在viewDidLoad中执行此操作会发生奇怪的事情:

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}