Swift 之横竖屏切换


1、核心方法 viewWillTransition

/// 切换横竖屏时,重设子view布局
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)
	}
	...
}

  • UIView 的宽高:
/// 定义
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

(2) 如何让某个 ViewController 保持竖屏?

  • 代码实现:

  • AppDelegate:

/// 全局变量
var isAllowAutorotate: Bool = true

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
 ...
 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)
  -> UIInterfaceOrientationMask {
    if isAllowAutorotate {
      return [.portrait, .landscapeLeft, .landscapeRight]
    }
    else {
      return .portrait
    }
  }
 ...
}
  • 想要保持竖屏的 ViewController:
override func viewWillAppear(_ animated: Bool) {
  super.viewWillAppear(animated)
  isAllowAutorotate = false
}

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)
  isAllowAutorotate = true
}
  • 参考资料:https://www.jianshu.com/p/816d024a4c6f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值