调整视图的高度



我有一个表,下面是bottomView。我的目标是在键盘向上时向上移动底部视图。但当我们调整键盘时,这个方法会把底部视图向上调用。我卡住了

- (void)keyboardWasShown:(NSNotification*)aNotification
{    
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    keyBoadSize =kbSize.height;
        [UIView animateWithDuration:0.2f animations:^{
        CGRect frame=bottomView.frame;
        keyBoardHeight = keyBoadSize;

        frame.origin.y -=keyBoadSize;
          bottomView.frame=frame;
        frame = tableViews.frame;
        frame.size.height -= keyBoardHeight;
        tableViews.frame = frame;

}

我也使用

- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat fixedWidth = textView.frame.size.width;
    CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = textView.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    textView.frame = newFrame;
    CGRect frame1 = [UIScreen mainScreen].bounds;
    bottomView.frame=CGRectMake(0, frame1.size.height-keyBoardHeight-20-newSize.height, self.view.frame.size.width, newSize.height+50);

}

首先需要在表视图中添加空间以允许滚动:

[self.tableView setContentInset:UIEdgeInsetsMake(0.0, 0.0, keyBoardheight, 0.0)];

然后动画自定义底部视图的Y位置。

作为旁注,在示例代码中keyBoadSize具有误导性。应该是keyBoardheight

PS:当键盘关闭时,不要忘记重置滚动空间:[self.tableView setContentInset:UIEdgeInsetsZero]; .

最新更新