设置为横向模式时视图位置不正确

发布于 2024-08-18 09:36:13 字数 2482 浏览 7 评论 0原文

我的 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; 
}

现在我已经让该视图始终以横向模式显示。但有一个问题。此视图的位置不正确,如此屏幕截图所示。

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

My iPhone application is navigation-based and contains a lot of portrait-only views and one landscape-only view for viewing images. So I'd like to force this landscape-only view to automatically rotate to lanscape even if device is positioned in portrait mode.

So here is what I'w done in that view's controller:

// 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; 
}

Now I've got that view always shown in lanscape mode. But there is one problem. This view is positioned incorrectly as shown in this screenshot.

I tried to manually set the frame after rotation but it didn't help. Also I didn't find a complete guide how to set one view to be landscape-only. Please help me with this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-08-25 09:36:13

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

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

Check out this guy's post. Basically you need to transform around the center of the window. Looks like you may also need to rotate the status bar.

A different method would be to use UIDevice setDeviceOrientation, but that's a private API and thus not a great solution.

疏忽 2024-08-25 09:36:13

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

[self.view sizeToFit];

Try using this method after you have applied the transformation:

[self.view sizeToFit];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文