ios – 方向更改后UIView框架未更新

ios – 方向更改后UIView框架未更新,第1张

概述使用 Swift 3后如何检测方向变化? 我需要在更改后检测它以计算帧大小. 编辑: 我问了这个问题,因为我需要重新定位方向更改的视图,就像这个问题:Can’t get background gradient to fill entire screen upon rotation 我不知道如何实现标记为正确答案的答案. 我尝试了另一个答案 self.backgroundImageView.laye 使用 Swift 3后如何检测方向变化?

我需要在更改后检测它以计算帧大小.

编辑:

我问了这个问题,因为我需要重新定位方向更改的视图,就像这个问题:Can’t get background gradient to fill entire screen upon rotation

我不知道如何实现标记为正确答案的答案.

我尝试了另一个答案

self.backgroundImageVIEw.layer.sublayers?.first?.frame = self.vIEw.bounds

但它不起作用.

在vIEwDIDLoad()中我有

let color1 =  UIcolor(red: 225.0/255.0,green: 210.0/255.0,blue: 0.0/255.0,Alpha: 1.0).cgcolor        let color2 = UIcolor(red: 255.0/255.0,green: 125.0/255.0,blue: 77.0/255.0,Alpha: 1.0).cgcolor        let gradIEntLayer = CAGradIEntLayer()        gradIEntLayer.colors = [color1,color2]        gradIEntLayer.locations = [ 0.0,1.0]        gradIEntLayer.frame = self.vIEw.bounds        self.vIEw.layer.insertSublayer(gradIEntLayer,at: 0)
解决方法 上面接受的答案在转换之前返回帧大小.所以你的视图没有更新.你需要在转换完成后获得帧大小.
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)    }
总结

以上是内存溢出为你收集整理的ios – 方向更改后UIView框架未更新全部内容,希望文章能够帮你解决ios – 方向更改后UIView框架未更新所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1104104.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存