视图在ipad上向上移动但不是每个控件都在iphone上移动

时间:2012-07-22 08:31:13

标签: iphone objective-c ipad ios5 ios-universal-app

我从堆栈溢出答案中实现了这个函数,首先回答Move view when so that keyboard does not hide text field

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    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 -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}

现在它在ipad上运行良好。我有一个文本框和旁边的一些按钮,上面是一个聊天的文本视图。您使用文本框键入聊天。在iPhone上,它会移动您输入的文本框和按钮,但它不会移动文本视图。实际上它会保持覆盖大部分窗口,而按钮会在它的中间顶部向上移动。

现在我刚刚创建了iphone故事板视图(现在有一个视图),并将我的所有控件连接到ipad上。现在该程序仍在ipad上正常工作,甚至可以在显示键盘上移动视图。但是在iphone上它除了主控制台之外还有一个UITextView。哪个是坏的关键是能够在键入时阅读文本:)我也想在这里理解这个理论。似乎我将视图定义为矩形并将其向上移动一些数字(如250),它应该全部移动。也许有更好的方法来做到这一点,比如首先获得键盘尺寸,但我仍然期望它能够移动那个高度。

麦克

1 个答案:

答案 0 :(得分:1)

我添加了对肖像和风景的支持,但修复是在我移动框架后将iPod作为主控制台(textview)重置为纵向(我唯一的iPod方向)的上下两个位置。整个视图被移动,所以我可以将它设置为相同的位置,它将在向上和向下移动。:

double tall = 88;
if(![self isTall])
tall =0;
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 -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
self.view.frame = rect;
if ( IDIOM != IPAD )
self._mainConsole.frame = CGRectMake(5, 5, 310, 379 + tall);
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
self.view.frame = rect;
if ( IDIOM != IPAD )
self._mainConsole.frame = CGRectMake(5, 5, 310, 379 + tall);
}