0

我希望我的文本字段应该在任何方向上移动。

但是我在横向和纵向颠倒时遇到问题。

当我将它定向到其中任何一个时,我的文本字段向下视图不会上升,但顶部的文本字段会上升。

建议我一些解决方案。

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

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

- (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];
}
4

1 回答 1

0

在您的 ViewController 中覆盖 - willRotateToInterfaceRotation:duration 方法。当用户旋转设备时调用此消息。在此方法中,您可以将所有视图设置为放置在适当的位置。请参阅参考中的响应视图旋转事件:UIViewController 参考

你可以这样做:

    - (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
}

然后对旋转时未正确放置的所有视图执行此操作

于 2011-08-11T12:17:54.413 回答