Проблема ориентации
Я хочу, чтобы мое текстовое поле двигалось вверх в любой ориентации.
Но у меня проблема с пейзажем справа и портретом вверх ногами.
Как и когда я ориентирую его на любое из них, мое текстовое поле вниз по представлению не идет вверх, а текстовое поле сверху идет вверх.
предложи мне какое-нибудь решение.
ниже приведен код, который я использую для ориентации.
Пожалуйста, предложите мне всю процедуру, если вы можете, так как я новичок в разработке для 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];
}
1 ответ
В вашем 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
}
а затем сделайте это для всех ваших видов, которые не правильно размещены при вращении