2011-08-11 73 views
0

我希望我的文本框在任何方向上向上移动。方向问题

但是我在横向右转和纵向倒置时出现问题。

因为当我将其定位到其中的任何一个文本框时,我的文本框向下看不到,但顶部的文本框正在向上。

建议我一些解决方案。

下面是我用于定位的代码。

请建议我整个过程,如果你可以,因为我是一个niwBie到iPhone开发。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField]; 
CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view]; 

CGFloat midLine=textFieldRect.origin.y+0.5*textFieldRect.size.height; 
CGFloat numerator=midLine-viewRect.origin.y-MINIMUM_SCROLL_FRACTION*viewRect.size.height; 
CGFloat denominator=(MAXIMUM_SCROLL_FRACTION-MINIMUM_SCROLL_FRACTION)*viewRect.size.height; 

CGFloat heightFraction=numerator/denominator; 
if(heightFraction < 0.0) 
{ 
    heightFraction=0.0; 
} 
else if(heightFraction > 1.0) 
{ 
    heightFraction=1.0; 
} 

CGRect viewFrame; 
UIInterfaceOrientation orientation=[[UIApplication sharedApplication]statusBarOrientation]; 
//code for text field editing in landscape an portrait mode 

if(orientation==UIInterfaceOrientationPortrait||orientation==UIInterfaceOrientationPortraitUpsideDown) 
{ 
    animatedDistance=floor(PORTRAIT_KEYBOARD_HEIGHT*heightFraction); 
} 
else 
{ 
    animatedDistance=floor(LANDSCAPE_KEYBOARD_HEIGHT*heightFraction); 
} 

if (orientation==UIInterfaceOrientationPortrait) { 
    viewFrame=self.view.frame; 
    viewFrame.origin.y-=animatedDistance; 
} 
else if(orientation==UIInterfaceOrientationPortraitUpsideDown){ 
    viewFrame=self.view.frame; 
    viewFrame.origin.y+=animatedDistance; 
} 
else if(orientation == UIInterfaceOrientationLandscapeRight){ 
    viewFrame=self.view.frame; 
    viewFrame.origin.x+=animatedDistance; 

} 
else if(orientation == UIInterfaceOrientationLandscapeLeft){ 

    viewFrame=self.view.frame; 
    viewFrame.origin.x-=animatedDistance; 
} 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
[self.view setFrame:viewFrame]; 

[UIView commitAnimations]; 
} 

回答

0

在你的ViewController中重写 - willRotateToInterfaceRotation:duration方法。当用户旋转设备时会调用此消息。在这种方法中,您可以将所有视图设置在适当的位置。请参阅响应视图旋转活动的参考:UIViewController reference

你可以做这样的事情:旋转

+0

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown){ yourView.frame = CGRectMake(x,y,w,h); //and set the proper coordinate for this view in this orientation } 

,然后做你所有的观点,即放置不正确,但我不知道如何在我的风景正确和颠倒模式下...视图中的文本字段正在用键盘覆盖...我将写入什么设置 - willRotateToInterfaceRotation:持续时间方法?...请通过查看我的代码以上解释我textFieldDidBeginEditing方法... O_o –

+0

请看上面。我添加了一些代码。 – broch

+0

其实在我的iPhone应用程序中,我使用上面的代码来提升视图以显示键盘上方的文本字段。我的应用程序支持所有四种接口方向。我使用下面的代码来解除视图。这里的问题是,它只适用于纵向。有没有什么办法可以在所有四个方向上实施正确的提升 –