【问题标题】:Manually rotate viewcontroller手动旋转视图控制器
【发布时间】:2017-03-19 15:44:24
【问题描述】:

我的应用绝对不能自动-旋转。但它包括一个屏幕,告诉用户旋转他的手机(而不是相反!)。 为此,ViewController 必须在屏幕显示时进行动画旋转(没有任何旋转事件)。

所以我用了

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:animated];
    [super viewWillDisappear:animated];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

按照每个网站和文档的建议,让我的屏幕旋转。

但只有 StatusBar 旋转:我的 NavigationBar 仍然卡在顶部。

【问题讨论】:

标签: iphone rotation


【解决方案1】:

我会在导航控制器视图上使用 CGAffineTransform 吗?只需使用动画块将其旋转 90 度?

【讨论】:

  • 哇,一点也不可爱,但它确实有效!我认为这应该是一种正确的方法,但至少,这是可行的。
【解决方案2】:

此代码有助于您自动调整导航栏的大小,您可以在创建 navigationController 和导航栏的地方使用它

  self.navigationController.navigationBar.autoresizesSubviews = YES;
  self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

如果不是,上面的代码将自动运行,那么您尝试这将在您需要更改的视图控制器的所有委托方法中运行

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:animated];
[self.navigationController shouldAutorotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
   CGRect frame = self.navViewController.navigationBar.frame;
   frame.size = self.view.frame.size;
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

    frame.size.height = 44;
} else {
    frame.size.height = 32;
}
self.navViewController.navigationBar.frame = frame;

如果导航控制器是 rootview 控制器,则检查它是否启用所有方向支持

   -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
[super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
[self.navigationController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
        return YES;
    }

您可以根据您的要求在下面列出的视图控制器委托中使用此代码

- (void)viewDidAppear:(BOOL)animated
- (void)viewWillAppear:(BOOL)animated
– willRotateToInterfaceOrientation:duration:

– willAnimateRotationToInterfaceOrientation:duration:

– didRotateFromInterfaceOrientation:

【讨论】:

  • WTF ? stackoverflow.com/questions/181780/…的第三个答案的复制粘贴@
  • 对不起朋友,不幸的是现在我编辑了它。这是我为您准备的原始帖子。我在我的模拟器中测试了该代码stackoverflow.com/questions/181780/…,而 iPhone 在模拟器中而不是在 iPhone 中工作,很抱歉给您带来不便。
  • 为什么调用 [self.navigationController shouldAutorotateToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation];在你的 viewDidLoad 中?这个方法应该只被覆盖,不是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多