1、核心方法 viewWillTransition
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
...
}
if size.width > size.height {
...
} else {
...
}
let orientation = UIApplication.shared.statusBarOrientation
switch orientation {
case .portrait, .portraitUpsideDown, .unknown:
...
case .landscapeLeft, .landscapeRight:
...
}
2、如何在横竖屏切换时更改 UI 细节?
- 以弹窗为例,UI 细节有:横屏居中显示,竖屏底部显示;弹窗高度,字体大小,UIView 的大小及 Offset。
- (1)约束方式:
重点:remakeConstraints
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
...
container.snp.remakeConstraints { make in
switch style {
case .horizontal:
make.center.equalToSuperview()
make.width.equalTo(config.containerWidth)
case .vertical:
make.bottom.equalTo(view.snp.bottom)
make.leading.trailing.equalToSuperview()
}
make.height.equalTo(config.containerHeight)
}
...
}
private var closeViewTopMargin: Constraint?
private var closeViewRightMargin: Constraint?
private var closeViewSize: Constraint?
closeView.snp.makeConstraints { make in
self.closeViewTopMargin = make.top.equalTo(background).offset(config.closeViewTopRightMargin).constraint
self.closeViewRightMargin = make.trailing.equalTo(background).offset(-config.closeViewTopRightMargin).constraint
self.closeViewSize = make.width.height.equalTo(config.closeViewSize).constraint
}
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
...
self.closeViewTopMargin?.update(offset: config.closeViewTopRightMargin)
self.closeViewRightMargin?.update(offset: -config.closeViewTopRightMargin)
self.closeViewSize?.update(offset: config.closeViewSize)
...
}
3、后续问题发现
(1) 如果在两个类中使用 viewWillTransition 不起作用
- 原因:当前的 ViewController 中复写了 viewWillTransition 方法,如果没有调用 super 方法,此消息不会再传递到下一个 ViewController 中。
- 解决方法:
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
...
}
- 参考资料:http://www.jb51.cc/iOS/337461.html
|