简体   繁体   English

检测lanscap模式并显示UIImage

[英]Detect the lanscap mode and show an UIImage

I am developing an iOS application with uses only portrait mode. 我正在开发仅使用纵向模式的iOS应用程序。 i would like to show in all the views of my application an image (320*480) when the user turn the iPhone ( lanscape mode) 我想在用户转动iPhone(横向模式)时在我的应用程序的所有视图中显示图像(320 * 480)

How i can do this ? 我该怎么做?

You can accomplish this in the UIViewController method didRotateFromInterfaceOrientation: , which will be called immediately after the user rotates the device. 您可以在UIViewController方法didRotateFromInterfaceOrientation:完成此操作,该方法将在用户旋转设备后立即调用。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (self.interfaceOrientation != UIInterfaceOrientationPortrait) {
        // add image to self.view
    } else {
        // remove image from self.view
    }
}

You could implement this method in each of your app's view controllers. 您可以在每个应用程序的视图控制器中实现此方法。 Alternatively, you could implement this in a custom subclass of UIViewController and then have all of your app's view controllers inherit from your custom view controller. 或者,您可以在UIViewController的自定义子类中实现此功能,然后让所有应用程序的视图控制器都从自定义视图控制器继承。

EDIT 编辑

As @David Dunham suggests, if all you are trying to do is prevent the app from changing the orientation when the user rotates the device, you can so so easily by implementing the UIViewController method shouldAutorotateToInterfaceOrientation: . 正如@David Dunham所建议的那样,如果您要做的就是阻止应用程序在用户旋转设备时更改方向,则可以通过实现UIViewController方法shouldAutorotateToInterfaceOrientation:轻松实现。 That method should return YES for every orientation you want to support, and NO for any that you do not wish to support. 对于您要支持的每个方向,该方法都应返回YES ,而对于不希望支持的任何方向,则返回NO

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // only allow portrait orientation
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

shouldAutorotateToInterfaceOrientation:返回NO会更容易吗?

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

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