定位问题

时间:2011-08-11 09:57:38

标签: iphone landscape-portrait orientation-changes

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

但是我在横向和纵向上面都有问题。

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

建议我解决一些问题。

下面的

是我用于定位的代码。

如果可以的话,请向我推荐整个程序,因为我是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];
}

1 个答案:

答案 0 :(得分:0)

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

中的响应查看轮换事件 你可以这样做:

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

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