iphone - 处理 ios 设备方向

标签 iphone ios uiview uideviceorientation

我的 uiview 有问题,具体取决于设备方向...

我遇到的主要问题是

UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown

弄乱了我的 View 我只想支持纵向和横向(左右)所以如果设备改变方向我的 View 会正确地改变自己..

这就是我目前实现的。基本上它是一个从屏幕底部向上滚动的 UIView,并且在这个 View 中有几个按钮,用户可以选择这些按钮来加载不同的 View 。

#pragma jumpBarButtons
- (void)jumpBarButtonPosition
{

    //orientation stuff
    if (jumpBar.isViewLoaded) {

        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];


        switch (orientation) {
            case UIDeviceOrientationPortrait:
            {
                NSLog(@"Portrait");

                // Row one
                jumpButton1.frame = CGRectMake(18.45, 23.0, 57.0, 57.0);
                jumpButton2.frame = CGRectMake(93.9, 23.0, 57.0, 57.0);
                jumpButton3.frame = CGRectMake(169.35, 23.0, 57.0, 57.0);
                jumpButton4.frame = CGRectMake(244.8, 23.0, 57.0, 57.0);
                // Row tow
                jumpButton5.frame = CGRectMake(18.45, 95.0, 57.0, 57.0);
                jumpButton6.frame = CGRectMake(93.9, 95.0, 57.0, 57.0);
                jumpButton7.frame = CGRectMake(169.35, 95.0, 57.0, 57.0);
                jumpButton8.frame = CGRectMake(244.8, 95.0, 57.0, 57.0);
                // Row three
                jumpButton9.frame = CGRectMake(18.45, 167.0, 57.0, 57.0);
                jumpButton10.frame = CGRectMake(93.9, 167.0, 57.0, 57.0);
                jumpButton11.frame = CGRectMake(169.35, 167.0, 57.0, 57.0);
                jumpButton12.frame = CGRectMake(244.8, 167.0, 57.0, 57.0);
                // Row four
                jumpButton13.frame = CGRectMake(18.45, 239.0, 57.0, 57.0);
                jumpButton14.frame = CGRectMake(93.9, 239.0, 57.0, 57.0);
                jumpButton15.frame = CGRectMake(169.35, 239.0, 57.0, 57.0);
                jumpButton16.frame = CGRectMake(244.8, 239.0, 57.0, 57.0);
            }
                break;

case UIDeviceOrientationLandscapeLeft:
            case UIDeviceOrientationLandscapeRight:
            {
                viewSpaceHeight = 207;
                viewSpaceWidth = 480;

                // Row one
                jumpButton1.frame = CGRectMake(19.7, 9.0, 57.0, 57.0);
                jumpButton2.frame = CGRectMake(96.42, 9.0, 57.0, 57.0);
                jumpButton3.frame = CGRectMake(173.13, 9.0, 57.0, 57.0);
                jumpButton4.frame = CGRectMake(249.84, 9.0, 57.0, 57.0);
                jumpButton5.frame = CGRectMake(326.55, 9.0, 57.0, 57.0);
                jumpButton6.frame = CGRectMake(403.26, 9.0, 57.0, 57.0);
                // Row tow
                jumpButton7.frame = CGRectMake(19.7, 75.0, 57.0, 57.0);
                jumpButton8.frame = CGRectMake(96.42, 75.0, 57.0, 57.0);
                jumpButton9.frame = CGRectMake(173.13, 75.0, 57.0, 57.0);
                jumpButton10.frame = CGRectMake(249.84, 75.0, 57.0, 57.0);
                jumpButton11.frame = CGRectMake(326.55, 75.0, 57.0, 57.0);
                jumpButton12.frame = CGRectMake(403.26, 75.0, 57.0, 57.0);
                // Row three
                jumpButton13.frame = CGRectMake(19.7, 141.0, 57.0, 57.0);
                jumpButton14.frame = CGRectMake(96.42, 141.0, 57.0, 57.0);
                jumpButton15.frame = CGRectMake(173.13, 141.0, 57.0, 57.0);
                jumpButton16.frame = CGRectMake(249.84, 141.0, 57.0, 57.0);

            }
                break;
//..

如果纵向有 16 个 4、4、4、4 图标,那就差不多了,如果横向,则图标为 6、6、4。如果设备面朝上或面朝下翻转会发生什么,所有这些按钮 View 都会消失。我该怎么做才能阻止这种情况,我们将不胜感激。

最佳答案

改用UIInterfaceDirection。它的一部分 UIApplication类,它只包含以下值

typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

您可以像这样轻松检查当前方向:

[[UIApplication sharedApplication] statusBarOrientation] 

关于iphone - 处理 ios 设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075600/

相关文章:

ios - iOS 应用程序更新是否需要 iPhone 6/6 Plus 支持?

iphone - iOS 中的 vtable 错误

ios - 从 UIActionSheet 呈现 UIPopover

ios - 在 iTunes 中更新后的相同应用程序

ios - 为什么初始化(帧: CGRect) not being called in my custom UIView class?

iphone - UIView 和 UIControl 在 UIScrollView 内拖动时的区别

iphone - iOS 钥匙串(keychain)安全

ios - 使用 Facebook iOS SDK 3.x 将视频上传到 Facebook

ios - 多个 UIView 动画

iphone - 如何从 EKEventStore 获取日历提供商(Google、Apple、Yahoo 等)