Swift-- UIViewController中使用到的willRotate父类方法

//UIViewController的willRotate父类方法使用

//以前工作中没有遇到这个需求,所以一直没了解这个父类的方法,用到后其实很简单
//在你的项目中可能会遇到翻转设备使其竖屏出现新的view,横屏对应的view都Hide		
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation,duration: TimeInterval) {
print("willRotate 旋转了")
        
        if toInterfaceOrientation.isLandscape{
            print("isLandscape")
            for v in self.view.subviews{
                if v.tag == 10{
                    v.removeFromSuperview()
                }else{
                    v.isHidden = false
                }
            }
        }else if toInterfaceOrientation.isPortrait {
            print("isPortrait")
            for v in self.view.subviews{
                v.isHidden = true
            }
            
            UIView.animate(withDuration: duration, animations: {
                let v = UIView()
                v.tag = 10
                v.frame = CGRect(x: 10, y: 10, width: UIScreen.main.bounds.width - 20, height: UIScreen.main.bounds.height - 20)
                self.view.addSubview(v)
                v.backgroundColor = UIColor.blue
            })

        }
}
----就这样实现了横竖屏view的切换,在多嘴一句:
       if UIDevice.current.orientation.isPortrait{
           print("UIDevice.current.orientation.isPortrait 鸡鸡--------")
       } else {
           print("UIDevice.current.orientation.isLandscape 鸡鸡-----")
       }
可判断横竖屏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值