Измените ориентацию для UIView, которая удалена из супервизора
В моем приложении у меня есть одна кнопка и два представления. Когда я нажал кнопку, я удалил первый вид из супервизора и добавил второе представление в супервизор в качестве подвью. Но проблема в том, что когда я изменил ориентацию устройства с портрета на пейзаж, а затем нажал кнопку, он покажет вид в портретном режиме, а не в ландшафтном режиме, и наоборот. Вот код, который я использую.
- (void)showFiles {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.25];
[UIView setAnimationTransition:([songTableView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:toggleButton cache:YES];
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
[songTableView setFrame:CGRectMake(0.0f, 44.0f, 480.0f, 256.0f)];
}
else {
[songTableView setFrame:CGRectMake(0.0f, 44.0f, 320.0f, 416.0f)];
}
if ([songTableView superview]) {
[toggleButton setImage:[UIImage imageNamed:@"AudioPlayerAlbumInfo.png"] forState:UIControlStateNormal];
}
else {
[toggleButton setImage:albumArtImageView.image forState:UIControlStateNormal];
}
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:([songTableView superview] ?
UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight)
forView:self.view cache:YES];
if ([songTableView superview])
{
albumArtImageView.image = albumArtImage;
albumArtImageView.contentMode = UIViewContentModeScaleAspectFit;
albumArtImageView.clipsToBounds = YES;
[songTableView removeFromSuperview];
[gradientLayer removeFromSuperlayer];
[self.view addSubview:uppercontrollsView];
[self.view addSubview:lowercontrollsView];
}
else
{
[albumArtImageView setImage:[UIImage imageNamed:@"AudioPlayerTableBackground.png"]];
albumArtImageView.contentMode = UIViewContentModeScaleAspectFill;
albumArtImageView.clipsToBounds = YES;
[uppercontrollsView removeFromSuperview];
[lowercontrollsView removeFromSuperview];
[self.view addSubview:songTableView];
[songTableView reloadData];
}
[UIView commitAnimations];
}
Ответы
Ответ 1
Вы можете использовать метод willRotateToInterfaceOrientation. Когда вы измените ориентацию устройства, он вызовет..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
[btn setFrame:CGRectMake(20, 52, 728, 617)];
}
else
{
[btn setFrame:CGRectMake(20, 52, 728, 617)];
}
}
Ответ 2
Если вы хотите обрабатывать iOS 6, то вы можете сделать следующее:
Если вы находитесь в режиме просмотра A, сохраните его InterfaceOrientation и затем
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return lastInterfaceOrientation;
}