1

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

并且还想知道这是否会被苹果商店接受?

谢谢

现在我从其他人那里得到了解决方案,如下所示

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

当你在那个时候添加这一行时,会出现一个警告,为了删除这个警告,只需在你的实现文件上添加下面的代码..

@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)


    - (void) setOrientation:(UIInterfaceOrientation)orientation;


    @end

然后在下面的方法中,如果需要,只需编写此代码..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    {

        return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    }

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

谢谢

4

5 回答 5

5

使用这条线以编程方式改变方向......

[[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);
}

我希望这对你有帮助..

:)

于 2012-09-27T06:29:15.873 回答
3

添加类变量

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];
}

这对我来说很好。

于 2012-09-27T06:32:25.153 回答
1

要将方向纵向模式更改为横向模式,请使用此代码

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

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

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
于 2012-09-27T07:06:28.100 回答
0

文档将方向属性描述为只读的,所以如果它有效,我不确定你是否可以依赖它在未来工作(除非 Apple 做了聪明的事情并改变了这一点;强制改变方向,不管如何用户当前拿着他们的设备,这是一个明显的功能需求)。

作为替代方案,在 viewDidLoad 中插入的以下代码将成功(并且有点奇怪)强制定向(假设您已经修改了 shouldAutorotateToInterfaceOrientation ):

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

显然,如果用户当前以纵向模式持有他们的设备(因此假设您的设备shouldAutorotateToInterfaceOrientation仅设置为横向,并且如果用户以纵向模式持有他们的设备,则此例程会将其转换为横向)。如果您仅设置为纵向UIDeviceOrientationIsPortrait,您只需交换。UIDeviceOrientationIsLandscapeshouldAutorotateToInterfaceOirentation

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

于 2012-09-27T06:15:15.820 回答
0

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

    float   angle = M_PI / 2; 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
   [ [self.view] setTransform:transform];
于 2012-09-27T06:51:38.330 回答