当移动容器UIView时调整UITableView的大小



我有一个应用程序,我在使用键盘时移动我的整个UIView,为了不隐藏一些重要的UITextField

我的问题是,UITableView,我有那里的行为不一样的方式作为所有其他视图。而不是移动整个UITableView,它只移动它的上边缘,这调整了UITableView的大小,这不是想要的效果。

我的问题在这里更好地描述:http://www.youtube.com/watch?v=AVJVMiBULEQ

这是我用来制作动画的代码:

-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];
    CGRect rect = self.view.frame;
    if (movedUp) {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= 140;
        rect.size.height += 140;
    } else {
        // revert back to the normal state.
        rect.origin.y += 140;
        rect.size.height -= 140;
    }
    self.view.frame = rect;
    [UIView commitAnimations];
}

正如上面的注释所解决的,上面的代码在视图没有自动调整大小的情况下工作。更简单的方法是使用CGAffineTransform转换来计算视图帧,而不是你。

-(void)setViewMovedUp:(BOOL)movedUp {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];
    if (movedUp) {
        self.transform = CGAffineTransformMakeTranslation(0, -150);//move up for 150 px
    } else {
        self.transform = CGAffineTransformIdentity;//reset to initial frame
    }
    [UIView commitAnimations];
}

最新更新