且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

方向更改后,UIView框架未更新

更新时间:2023-02-12 07:53:42

上面接受的答案会在过渡之前返回帧大小.因此您的视图不会更新.在过渡完成后,您需要获取帧大小.

The above accepted answer returns frame size before transition.So your view is not updating..You need to get the frame size after the transition has been completed.

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

        coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) -> Void in

            let orient = UIApplication.shared.statusBarOrientation

            switch orient {

            case .portrait:

                print("Portrait")

            case .landscapeLeft,.landscapeRight :

                print("Landscape")

            default:

                print("Anything But Portrait")
            }

            }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
                //refresh view once rotation is completed not in will transition as it returns incorrect frame size.Refresh here           

        })
        super.viewWillTransition(to: size, with: coordinator)

    }