" interfaceOrientation"在iOS 8中已弃用,如何更改此方法目标C.

时间:2015-02-28 21:31:50

标签: ios objective-c cocoa-touch

我从Cocoa Controls下载了一个使用此方法的simpleCamera视图

- (AVCaptureVideoOrientation)orientationForConnection
{
    AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
    switch (self.interfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIInterfaceOrientationLandscapeRight:
            videoOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        default:
            videoOrientation = AVCaptureVideoOrientationPortrait;
            break;
    }
    return videoOrientation;
}

问题是在iOS 8中不推荐使用“interfaceOrientation”但我不知道在Switch条件中替换它的方法和方法。

5 个答案:

答案 0 :(得分:141)

获取设备方向不正确。例如,设备可能处于横向模式,但仅支持纵向的视图控制器将保留为纵向。

相反,请使用:

void checkData()
    {
        SuspendLayout();
        try
        {
            MySqlConnection conn = new MySqlConnection(myConnection);
            conn.Open();
            MySqlCommand command = new MySqlCommand("SELECT * FROM maindatabase.animelist where TitleAnime=?Title;", conn);
            //command.Parameters.AddWithValue("?CN", int.Parse(a.ToString()));
            command.Parameters.AddWithValue("?Title", textBox3.Text);
            MySqlDataReader reader = command.ExecuteReader();

            int ctr = 0;
            while (reader.Read())
            {
                ctr++;

            }
            if (ctr == 1)
            {
                my = Form.ActiveForm as MyList;
                my.msg = new Message_Box();
                my.msg.Descrip.Text = "Record is already in the Database";
                my.msg.Title.Text = "Duplicate Record";
                my.msg.ShowDialog();
            }
            else
            {

                updateData();
            }


            conn.Close();
            ResumeLayout();
        }
        catch (Exception ex)
        {
            MessageBox.Show("" + ex);
        }
    }

另一个好处是它仍然使用UIInterfaceOrientation枚举,因此您的代码很少需要更改。

答案 1 :(得分:19)

从iOS8开始,Apple建议使用TraitCollections(Size Classes)而不是interfaceOrientation。

此外,由于iOS 9和新iPad功能"多任务",在某些情况下设备方向与窗口比例不相符! (打破你的应用程序用户界面)

所以你在使用普通尺寸课时也应该非常小心,因为它不需要占用整个iPad屏幕。

有时,TraitCollections并不能满足您的所有设计需求。对于这些情况,Apple建议比较视图的界限:

if view.bounds.size.width > view.bounds.size.height {
    // ...
}

我很惊讶,但您可以在21 15上查看WWDC 2015视频Getting Started with Multitasking on iPad in iOS 9

也许你真的在寻找设备方向而不是屏幕或窗口比例。如果您关心设备摄像头,则不应使用TraitCollection,也不应使用视图边界。

答案 2 :(得分:2)

使用UIDevice的-orientation属性不正确(即使它可以在大多数情况下工作)并且可能导致一些错误,例如UIDeviceOrientation也考虑设备的方向(如果是面朝上或朝下,UIInterfaceOrientation枚举中没有对这些值的对 此外,如果您将应用程序锁定在某个特定方向,UIDevice将为您提供设备方向,而不考虑这一点 另一方面,iOS8已弃用interfaceOrientation类的UIViewController属性。
有两个选项可用于检测界面方向:

  • 使用状态栏方向
  • 在iPhone上使用大小类,如果它们没有被覆盖,它们可以让您了解当前的界面方向

仍然缺少一种理解界面方向变化方向的方法,这在动画制作过程中非常重要。
在WWDC 2014的会议中"查看iOS8中的控制器进展"说话者也使用替换-will/DidRotateToInterfaceOrientation的方法提供了解决该问题的方法。

这里提出的解决方案部分实施:

-(void) viewWillTransitionToSize:(CGSize)s withTransitionCoordinator:(UIVCTC)t {
    orientation = [self orientationFromTransform: [t targetTransform]]; 
    oldOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    [self myWillRotateToInterfaceOrientation:orientation duration: duration]; 
    [t animateAlongsideTransition:^(id <UIVCTCContext>) {
         [self myWillAnimateRotationToInterfaceOrientation:orientation
                                                  duration:duration];
      }
      completion: ^(id <UIVCTCContext>) {
         [self myDidAnimateFromInterfaceOrientation:oldOrientation];
      }];
}

答案 3 :(得分:1)

Swift 4+版

UIApplication.shared.statusBarOrientation

答案 4 :(得分:0)

使用UIApplication.shared.statusBarOrientation时,Xcode 11将显示警告'statusBarOrientation' was deprecated in iOS 13.0: Use the interfaceOrientation property of the window scene instead.

此答案在Swift 5中,并且支持iOS 13和更低版本。它假定以下条件:

  • 您将仅创建一个场景,并且最多只能创建一个场景
  • 您希望将代码包含在特定视图中
  • 您的视图具有类型为previewLayer的成员变量AVCaptureVideoPreviewLayer

首先,在您的SceneDelegate.swift中,添加以下几行:

    static var lastCreatedScene: UIWindowScene?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let newScene = (scene as? UIWindowScene) else { return }
        Self.lastCreatedScene = newScene
    }

然后在您的视图中,添加以下函数并在layoutSubviews()中调用它:

    func refreshOrientation() {
        let videoOrientation: AVCaptureVideoOrientation
        let statusBarOrientation: UIInterfaceOrientation
        if #available(iOS 13, *) {
            statusBarOrientation = SceneDelegate.lastCreatedScene?.interfaceOrientation ?? .portrait
        } else {
            statusBarOrientation = UIApplication.shared.statusBarOrientation
        }
        switch statusBarOrientation {
        case .unknown:
            videoOrientation = .portrait
        case .portrait:
            videoOrientation = .portrait
        case .portraitUpsideDown:
            videoOrientation = .portraitUpsideDown
        case .landscapeLeft:
            videoOrientation = .landscapeLeft
        case .landscapeRight:
            videoOrientation = .landscapeRight
        @unknown default:
            videoOrientation = .portrait
        }
        self.previewLayer.connection?.videoOrientation = videoOrientation
    }