UIViewController traitCollection自动旋转问题

时间:2018-11-18 17:55:13

标签: ios uiviewcontroller uikit auto-rotation uitraitcollection

我正在使用statusBarOrientation确定UIViewController的方向,并在函数viewWillTransition(to size:...)中相应地配置视图

   override var supportedInterfaceOrientations: UIInterfaceOrientationMask {

    var orientations:UIInterfaceOrientationMask = .landscapeRight

    if  !self.disableAutoRotation {
        switch Settings().orient {
        case .landscape:
            orientations = .landscape
        case .portrait:
            orientations = .portrait
        default:
            orientations = .all
        }
    }



    return orientations
}

 override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

    let orientation = UIApplication.shared.statusBarOrientation

    NSLog("View will transition to \(size), \(orientation.rawValue)")
    super.viewWillTransition(to: size, with: coordinator)


    coordinator.animate(alongsideTransition: { [unowned self] (_) in

    ...
    }
}

问题:即使此视图控制器的视图不在顶部,也会调用此函数。有时我会根据用户设置将此视图控制器的旋转限制为纵向或横向。此视图控制器提供的其他视图控制器可以自由旋转到任何方向。并且当它们旋转时,它们也会干扰此视图控制器。如何防止这种情况发生?

0 个答案:

没有答案