gpt4 book ai didi

ios - 如何将 View 从纵向模式旋转到横向模式

转载 作者:行者123 更新时间:2023-11-29 10:50:38 25 4
gpt4 key购买 nike

我的应用程序中存在方向问题。假设我有两个 View (带有专用 View Controller ):

  • first 应该纵向显示(正确显示)
  • second 应该横向显示(显示不正确)

它被缩窄并以纵向显示(如下面的第二张图片所示)。当我水平旋转设备并返回纵向时,一切正常。但在推送 View 后,它显示不正确(下图)。我该如何解决这个问题?

enter image description here

我使用 CustomNavigationController whish 继承自 UINavigatorControler 并实现三个方法:

- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}

在应用程序委托(delegate)中,我以这种方式初始化 Controller :

self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];

第一个 View Controller 以这种方式实现方向功能:

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait)
return YES;

return NO;
}

第二个 View Controller 以这种方式实现方向功能:

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeRight)
return YES;

return NO;
}

最佳答案

hi 声明一个全局变量 BOOL isLandScape ;

initialize it as isLandScape=NO;


- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{

if ((orientation == UIInterfaceOrientationLandscapeRight)||(orientation == UIInterfaceOrientationLandscapeLeft))
{

isLandScape=YES;

return YES;

}

else

{

isLandScape=NO;

return NO;

}

yourObject.frame=CGRectMake(isLandScape?0:0,isLandScape?0:0,isLandScape?1024:768,isLandScape?768:1024);


}

关于ios - 如何将 View 从纵向模式旋转到横向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com