使用
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) }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。