-1

我该怎么做whatsapp风格,当点击消息空间时,它会从底部向上推动键盘,以及向上推动工具栏。然后当取消时(即在后台单击),它将使用工具栏将键盘向下推

4

1 回答 1

2

这是通过 U IKeyboardWillShowNotificationUIKeyboardDidShowNotification和通知完成UIKeyboardWillHideNotification的。UIKeyboardDidHideNotification然后,当您处理通知时,您会调整框架的高度:

例如:

- (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];
}
于 2012-12-06T11:31:22.290 回答