简体   繁体   中英

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. 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 :

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

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return lastInterfaceOrientation;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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