我希望我的文本字段应该在任何方向上移动。
但是我在横向和纵向颠倒时遇到问题。
当我将它定向到其中任何一个时,我的文本字段向下视图不会上升,但顶部的文本字段会上升。
建议我一些解决方案。
下面是我用于定位的代码。
如果可以的话,请建议我整个过程,因为我是 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];
}