【问题标题】:View positioned incorrectly when set to landscape mode设置为横向模式时视图位置不正确
【发布时间】:2010-01-18 13:17:08
【问题描述】:

我的 iPhone 应用程序是基于导航的,包含许多仅纵向视图和一个仅用于查看图像的横向视图。因此,即使设备处于纵向模式,我也想强制此仅横向视图自动旋转为横向。

这就是我在该视图的控制器中所做的:

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
}

- (void)viewWillAppear:(BOOL)animated
{
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
 [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

 _originalTransform = [[appDelegate navigationController].view transform];
 _originalBounds = [[appDelegate navigationController].view bounds];
 _originalCenter = [[appDelegate navigationController].view center];

 CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
 landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);

 [[appDelegate navigationController].view setTransform:landscapeTransform];

 [appDelegate navigationController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
 [appDelegate navigationController].view.bounds  = CGRectMake(0.0, 0.0, 480.0, 320.0);
 [appDelegate navigationController].view.center  = CGPointMake (240.0, 160.0);

 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
 }
}

- (void)viewWillDisappear:(BOOL)animated
{ 
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
 [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 [[appDelegate navigationController].view setTransform:_originalTransform];
 [[appDelegate navigationController].view setBounds:_originalBounds];
 [[appDelegate navigationController].view setCenter:_originalCenter];

 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; 
}

现在我的视图总是以横向模式显示。但是有一个问题。此视图定位不正确,如this screenshot 所示。

我尝试在旋转后手动设置框架,但没有帮助。此外,我没有找到完整的指南,如何将一个视图设置为仅限横向。 请帮我解决这个问题。

【问题讨论】:

    标签: iphone landscape uinavigationitem


    【解决方案1】:

    看看这个人的post。基本上你需要围绕窗口的中心进行变换。看起来您可能还需要旋转状态栏。

    另一种方法是使用 UIDevice setDeviceOrientation,但这是一个私有 API,因此不是一个很好的解决方案。

    【讨论】:

    • 谢谢,我尝试了很多解决方法,它们帮助了我。但是在横向对话框上方显示的纵向模式对话框仍然存在一个问题。所以几个小时后,我决定让最初的对话是纵向和横向的。这解决了我所有的麻烦。归咎于苹果。
    【解决方案2】:

    在应用转换后尝试使用此方法:

    [self.view sizeToFit];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多