Изменение UIView на изменении ориентации

Эй все. У меня есть довольно простой вопрос. Я разрабатываю "богатое" приложение для iPad, и у меня есть два фоновых изображения, специально предназначенные для среды и портрета. Я хотел бы, чтобы этот ImageView автоматически изменился в зависимости от ориентации устройств. (как в значительной степени все приложения для iPad Яблок).

Кто-либо может указать на меня в правильном направлении? Я предполагаю, что это было бы что-то, что я делаю на viewDidLoad..

12
задан Designeveloper 29 April 2010 в 03:11
поделиться

2 ответа

Лучшее, что вы можете сделать, - это изменить фреймы фреймов подпредставления в соответствии с ориентацией вашего интерфейса. Вы можете сделать это так:

 #pragma mark -
 #pragma mark InterfaceOrientationMethods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationIsPortrait(interfaceOrientation) || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){
        //change the view and subview frames for the portrait view
    }
    else{   
        //change the view and subview  frames for the landscape view
    }

    [UIView commitAnimations];
}

Надеюсь, это поможет.

22
ответ дан 2 December 2019 в 03:48
поделиться

Я нашел очень простой альтернативный способ обойти это. Поскольку я просто меняю фоновое изображение, добавляю это ..

`

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation ==
        UIInterfaceOrientationPortraitUpsideDown) { 
        [brownBackground setImage:[UIImage imageNamed:@"Portrait_Background.png"]];
    } else {
        [brownBackground setImage:[UIImage imageNamed:@"Landscape_Background.png"]];
    }
}

`

Изменяет фон объявленного UIImageView в зависимости от ориентации. Единственным недостатком является то, что текущее фоновое изображение не отображается в построителе интерфейса, поскольку оно обрабатывается с помощью кода.

9
ответ дан 2 December 2019 в 03:48
поделиться