手动旋转viewcontroller

时间:2012-01-11 15:55:00

标签: iphone rotation

我的应用一定不能自动 - 旋转。但它包括一个屏幕,告诉用户旋转他的手机(而不是相反!)。 要做到这一点,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仍然位于顶部。

2 个答案:

答案 0 :(得分:0)

我可能会在navigationcontroller视图中使用CGAffineTransform吗?只需使用90度的动画块旋转它吗?

答案 1 :(得分:0)

此代码有助于您自动调整导航栏的大小,您可以在创建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;
    }

您可以根据您的要求在下面列出的viewcontroller委托中使用此代码

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

– willAnimateRotationToInterfaceOrientation:duration:

– didRotateFromInterfaceOrientation: