繁体   English   中英

如何在不更改iPhone应用程序中的设备方向的情况下更改应用程序的方向

[英]How to change the orientation of the app without changing the device orientation in iphone app

我想更改应用程序的方向而不更改iPhone应用程序中的设备方向。 我想以编程方式将视图从portraid模式更改为landscap模式。

而且还想知道这将被苹果​​商店接受吗?

谢谢

现在我从其他人那里得到了以下解决方案

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

当您在那时添加此行时,会出现一个警告,要删除此警告,只需在实现文件中添加以下代码。

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)


    - (void) setOrientation:(UIInterfaceOrientation)orientation;


    @end

然后在波纹管方法中,如果需要,只需编写此代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    }

但是现在想知道这是否被苹果应用商店接受了吗?

谢谢

使用此行以编程方式更改方向...

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

并且在那时添加此行时,会出现一个警告,要删除此警告,只需在实现文件中添加以下代码。

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end

然后在波纹管方法中,如果需要,只需编写此代码。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //    return NO;
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

希望对您有帮助。

:)

添加一个类变量

Bool isInLandsCapeOrientation;

在viewDidLoad中

将此标志设置为

isInLandsCapeOrientation = false;

添加以下功能

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     if (!isInLandsCapeOrientation) {
         return (UIInterfaceOrientationIsPortrait(interfaceOrientation));

     }else {
         return  (UIInterfaceOrientationIsLandscape(interfaceOrientation));
     }
}

要将方向从纵向更改为横向,请通过按钮操作进行

 - (IBAction)changeOrientationButtonPressed:(UIButton *)sender
{
    isInLandsCapeOrientation = true;
    UIViewController *viewController = [[UIViewController alloc] init];
    [self presentModalViewController:viewController animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}

这对我来说很好。

要将定向写意模式更改为横向模式,请使用此代码

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

使用此代码以编程方式更改方向...

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

该文档将方向属性描述为只读,因此,如果它可以工作,我不确定您将来是否可以依靠它来工作(除非Apple做得很聪明,并且对此进行了更改;无论方向如何变化,都强制改变方向)用户当前正在握住他们的设备,这是一项显而易见的功能需求)。

另外,插入到viewDidLoad中的以下代码将成功(有点奇怪)强制方向(假设您已经修改过,应该是AutorotateToInterfaceOrientation):

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

显然,如果用户当前以纵向方向握住其设备,则可以执行此操作(因此,大概应该将您的shouldAutorotateToInterfaceOrientation设置为仅横向使用,如果用户以纵向模式握住其设备,则此例程会将其转换为横向)。 如果您的shouldAutorotateToInterfaceOirentation仅设置为纵向,则只需将UIDeviceOrientationIsPortraitUIDeviceOrientationIsLandscape交换即可。

出于某种原因,从主窗口中删除该视图,然后重新添加该视图会强制其查询shouldAutorotateToInterfaceOrientation并正确设置方向。 鉴于这不是Apple认可的方法,也许应该避免使用它,但是它对我有用。 你的旅费可能会改变。 但这也涉及其他技术。 检查SO讨论

如果您只想更改横向的特定视图,则可以在其viewDidLoad中尝试以下操作

    float   angle = M_PI / 2; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
   [ [self.view] setTransform:transform];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM