简体   繁体   English

更改从超级视图中删除的UIView的方向

[英]Change the Orientation for UIView that is removed from the 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];

} }

You can use willRotateToInterfaceOrientation method. 您可以使用willRotateToInterfaceOrientation方法。 When you change device orientation it will call.. 当您更改设备方向时,它将调用..

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

}

    }

If you want to handle iOS 6, what you can do is : 如果您想使用iOS 6,可以执行以下操作:

If you're on view A, then store its InterfaceOrientation and then 如果您在视图A上,则存储其InterfaceOrientation,然后

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return lastInterfaceOrientation;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM