旋转视图时不触发 UIInterfaceOrientationIsLandscape

发布于 2024-10-25 20:51:37 字数 586 浏览 7 评论 0原文

我正在尝试使我的应用程序能够横向和纵向工作。我在所有视图控制器中安装了正确的方法,并且当 ipad 模拟器旋转时此代码会触发,但总是触发纵向。景观 IF 子句永远不会被触发。我可能做错了什么?

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)InterfaceOrientation {
NSLog(@"******* ROTATING ******");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {

        NSLog(@"ROTATING View Landscape ******");

    } else    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {

        NSLog(@"ROTATING View Portrait ******");

    }
} 

}

I am trying to make my app work in landscape orientation as well as portrait. I have the proper methods installed in all my viewcontrollers and this code triggers when the ipad simulator is rotated, but always triggers portrait. The landscape IF clause never gets triggered. What could I be doing wrong?

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)InterfaceOrientation {
NSLog(@"******* ROTATING ******");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {

        NSLog(@"ROTATING View Landscape ******");

    } else    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {

        NSLog(@"ROTATING View Portrait ******");

    }
} 

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

皇甫轩 2024-11-01 20:51:37

设备和界面方向似乎是不同的东西。我在 Apple 网站上找到了使用 UIDeviceOrientation 执行此操作的方法。使用此测试,当我旋转模拟器和我的 iPad 设备时,我会得到正确的触发器。

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

if (UIDeviceOrientationIsLandscape(deviceOrientation))

{


}

else if (deviceOrientation == UIDeviceOrientationPortrait)

{

}    

Device and interface orientation appear to be different things. I found on Apple's site the way to do this using UIDeviceOrientation. Using this test I get the proper triggers when i rotate the simulator and my ipad device.

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

if (UIDeviceOrientationIsLandscape(deviceOrientation))

{


}

else if (deviceOrientation == UIDeviceOrientationPortrait)

{

}    
夜夜流光相皎洁 2024-11-01 20:51:37

我发现检测 iPhone/iPad 当前设备方向的最可靠方法是使用 statusBarOrientation 属性,如下所示:

UIInterfaceOrientation currentOrientation = 
    [UIApplication sharedApplication].statusBarOrientation;

如果您使用它而不是 self.interfaceOrientation< /code>,你的代码可以工作吗?

I've found that the most reliable way to detect the current device orientation of an iPhone/iPad is to use the statusBarOrientation property, like so:

UIInterfaceOrientation currentOrientation = 
    [UIApplication sharedApplication].statusBarOrientation;

If you use that instead of self.interfaceOrientation, does your code work?

尸血腥色 2024-11-01 20:51:37

这是我在 swift for i:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    let  deviceOrientation:UIDeviceOrientation = UIDevice.currentDevice().orientation

    if (UIDeviceOrientationIsPortrait(deviceOrientation)){
        let value = UIInterfaceOrientation.LandscapeLeft.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }
}

viewWillTransitionToSize:withTransitionCoordinator: 方法中的代码,用于确定您是旋转到纵向还是横向。

当检测到纵向模式时,我的代码基本上将方向更改回横向模式。

Here is my code in swift for i:

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    let  deviceOrientation:UIDeviceOrientation = UIDevice.currentDevice().orientation

    if (UIDeviceOrientationIsPortrait(deviceOrientation)){
        let value = UIInterfaceOrientation.LandscapeLeft.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")
    }
}

viewWillTransitionToSize:withTransitionCoordinator: method above to figure out whether you are rotating to portrait or landscape.

my code basically changes the orientation back to landscape mode when portrait mode is detected.

浅浅淡淡 2024-11-01 20:51:37

使用:

+(bool)isLandscape
{
    return
        !UIDeviceOrientationIsPortrait( [UIDevice currentDevice].orientation ) ||
        !UIInterfaceOrientationIsPortrait( [UIDevice currentDevice].orientation );
}

Use:

+(bool)isLandscape
{
    return
        !UIDeviceOrientationIsPortrait( [UIDevice currentDevice].orientation ) ||
        !UIInterfaceOrientationIsPortrait( [UIDevice currentDevice].orientation );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文