簡體   English   中英

更改從超級視圖中刪除的UIView的方向

[英]Change the Orientation for UIView that is removed from the superview

在我的應用程序中,我有一個按鈕和兩個視圖,點擊按鈕時,我從超級視圖中刪除了第一個視圖,並將超級視圖中的第二個視圖添加為子視圖。 但是問題是,當我將設備方向從縱向更改為橫向,然后點擊按鈕時,它將以縱向視圖而不是橫向模式顯示視圖,反之亦然。 這是我正在使用的代碼。

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

}

您可以使用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)];

}

    }

如果您想使用iOS 6,可以執行以下操作:

如果您在視圖A上,則存儲其InterfaceOrientation,然后

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return lastInterfaceOrientation;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM