2011-12-10 18 views
1

我有一個應用程序,在使用鍵盤時我將整個UIView向上移動,以避免隱藏一些重要的UITextFieldUITableView在移動容器時調整大小UIView

我的問題是,我在那裏的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]; 
} 
+0

嗯,我總是通過設置變換self.transfom = CGAffineTransformMakeTranslation(0,-140)和self.transform = CGAffineTransformIdentity(向下移動)做到這一點。你可以試試這個,看看你是否得到相同的結果? –

+0

謝謝。這工作,但它立即移動,沒有任何動畫。有什麼方法可以爲此添加動畫嗎? – Dimme

+0

通過在Interface Builder中更改自動調整選項來解決。 =)它堅持調整大小。 – Dimme

回答

1

如上意見解決,代碼時,視圖不會自動調整上述作品。更簡單的方法是使用計算視圖框架的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]; 
}