Iphone 将屏幕从纵向旋转到横向

Iphone 将屏幕从纵向旋转到横向,iphone,rotation,Iphone,Rotation,我使用在web中找到的以下代码将屏幕旋转到横向模式。我不明白他们打算做什么。特别是它设置的边界。有人能解释一下它在做什么吗 if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) { CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; [[UIA

我使用在web中找到的以下代码将屏幕旋转到横向模式。我不明白他们打算做什么。特别是它设置的边界。有人能解释一下它在做什么吗

if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
    CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];


    UIScreen *screen = [UIScreen mainScreen];
    CGRect newBounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width - statusBarFrame.size.height);



    self.navigationController.view.bounds = newBounds;
    self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);
    self.navigationController.view.transform = CGAffineTransformConcat(self.navigationController.view.transform, CGAffineTransformMakeRotation(degreesToRadian(90)));
    self.navigationController.view.center = window.center;
}

您的rootView的大小过去是320480例如,旋转后,您应该将其设置为480320以适应横向模式下的屏幕,这就是您需要更改视图边界的原因

设置变换使视图实际旋转90度

UIKit在为您自动旋转时也在做类似的事情。

你好,Janaka

         You will try this code.

         Take a look at the function `shouldAutorotateToInterfaceOrientation`: in the UIViewController class. This function returns YES if the orientations is supported by your UIView. If you return YES only to the landscape orientation, then the iPhone will automatically be put in that orientation.
下面的代码应该可以做到这一点。将其置于UIViewController中,该控制器控制要置于横向模式的视图

使用这种方法。 -BOOLshouldAutorotateToInterfaceOrientation:UIInterfaceOrientation接口方向{ //对于支持的方向返回YES 返回interfaceOrientation==UIInterfaceOrientationLandscape; }

试试这个链接,它对你绝对有帮助

如果让窗口和UIViewController自己应用转换,然后通过在视图控制器上实现适当的方法来处理您需要的任何视图更改,您会得到更好的服务。根据我们的要求,我们必须手动进行转换,没有理由手动将转换应用于导航控制器。你最终只会朝自己的脚开枪。然而,它所做的是将变换矩阵应用于具有90度旋转的视图。如果您这样做,您的边界将是错误的,它们对于新屏幕高度来说太长,对于新屏幕宽度来说太窄。上面的代码更新边界以适应新屏幕,将中心重新定位到屏幕新逻辑中心的位置,围绕视图中心应用旋转,然后将中心移回屏幕中心我们正在将其应用到导航控制器的视图,对吗?不是导航控制器对吗?是的,没错。导航控制器会因为你这样做而非常不高兴;那么边界设置了新的显示区域?是的。通常情况下,根视图应始终与屏幕大小相同。@Janaka如果您认为答案有帮助,请向上投票并接受。我希望视图永久处于横向模式。我不希望它是基于设备的改变。
#define degreesToRadians(x) (M_PI * x / 180.0)

- (void)viewWillAppear:(BOOL)animated
{

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];

    CGRect newBounds = CGRectMake(0, 0, 480, 320);
    self.navigationController.view.bounds = newBounds;
    self.navigationController.view.center = CGPointMake(newBounds.size.height / 2.0, newBounds.size.width / 2.0);

    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(90));

    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.view.transform = CGAffineTransformIdentity;
    self.navigationController.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320.0, 480.0);

    [super viewWillDisappear:animated];
}