更改从superview中删除的UIView的方向 [英] Change the Orientation for UIView that is removed from the superview

查看:83
本文介绍了更改从superview中删除的UIView的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个按钮和两个视图,当我点击按钮时,我从superview中删除了第一个视图,并在superview上添加第二个视图作为子视图。但问题是,当我将设备方向从纵向更改为横向,然后点击按钮时,它将在纵向视图中显示视图而不是横向模式,反之亦然。这是我正在使用的代码。

In my App, i have a single button and two Views, When i tapped the button, i removed the First View from the superview and add the second view on the superview as the subview. But the problem is that when i changed the device orientation from portrait to landscape, and then tapped the button, it will show the view in the portrait view not in the landscape mode and vice versa. Here is the code which i am using.

    - (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];

}

推荐答案

如果你想处理iOS 6,你可以做的是:

If you want to handle iOS 6, what you can do is :

如果你在视图A上,那么存储它的InterfaceOrientation然后

If you're on view A, then store its InterfaceOrientation and then

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return lastInterfaceOrientation;
}

这篇关于更改从superview中删除的UIView的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆