swift 屏幕的翻转 + 状态栏(statusBar)的隐藏
阅读原文时间:2023年07月11日阅读:1

1.状态栏的隐藏

这个问题是基于 UIApplication.shared.isStatusBarHidden = true; 调用居然是无效的……

现在写下自己的代码,用来备忘吧……

1.首先需要复写一个 hidden 的这个属性 ,然后调用 setNeedsStatusBarAppearanceUpdate() 方法,

这样使用又觉得麻烦,所以 又多设置了一个变量 ,让使用更简单

override var prefersStatusBarHidden: Bool {
return self.isStatusBarHidden
}

var isStatusBarHidden = false {

    didSet{  
        self.setNeedsStatusBarAppearanceUpdate()  
    }  
}

在使用的地方调用

self.isStatusBarHidden = true
// self.isStatusBarHidden = false;

2.屏幕的翻转:

首先写下试用与 iphone 和 ipad 的

我使用的是transfrone 旋转 视图view ,这样的前提是 这个view 是present 出来的,不能用 navigationController 了

//定义枚举
enum ScreenOrientation :Int {

case portrait = ;  
case landscape =  

}

// 定义常量
let scrw = UIScreen.main.bounds.size.width;
let scrh = UIScreen.main.bounds.size.height;

//定义方法
func tranformView() -> Void {

    if self.orientation == .landscape {  
        self.orientation = .portrait  
    }else{  
        self.orientation = .landscape;  
    }

    if self.orientation == .landscape {

        self.isStatusBarHidden = true

        UIView.animate(withDuration: deviceChangeOrientationTimeIntravel, animations: {

            self.view.transform = CGAffineTransform.init(rotationAngle: CGFloat(Double.pi / ))  
            self.view.bounds = CGRect(x:,y:,width:scrh,height:scrw);  
            self.viewWillLayoutSubviews();  
            self.view.layoutIfNeeded();  
        }) { (isFinish) in

        }

    }else{

        self.isStatusBarHidden = false;

        UIView.animate(withDuration: deviceChangeOrientationTimeIntravel, animations: {

            self.view.transform = CGAffineTransform.init(rotationAngle: CGFloat())  
            self.view.bounds = CGRect(x:,y:,width:scrw,height:scrh);  
            self.viewWillLayoutSubviews();  
            self.view.layoutIfNeeded();

        }) { (isFinish) in

        }  
    }  
}

简单易懂,应该是可以使用了……

网上也找了 这个方法,但是使用的时候,只有在iphone 上用,ipad 使用无效

/*
创建个 extension
使用这个 扩展 要在 appdelegate 文件中 添加代码:
var blockRotation: Bool = false
*/

extension AppDelegate{
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if self.blockRotation == true{

        return UIInterfaceOrientationMask.all;  
    }else{

        return UIInterfaceOrientationMask.portrait;  
    }  
}  

}

调用的时候

let appDelegate = UIApplication.shared.delegate as! AppDelegate
//横屏
@objc func hengp()->Void{

    appDelegate.blockRotation = true  
    let value = UIInterfaceOrientation.landscapeLeft.rawValue  
    UIDevice.current.setValue(value, forKey: "orientation");  
}  
//竖屏  
@objc func shup() -> Void{  
    appDelegate.blockRotation = false  
    let value = UIInterfaceOrientation.portrait.rawValue  
    UIDevice.current.setValue(value, forKey: "orientation")  
}

//切换横竖屏 执行的代理方法

override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {

    print("方向即将改变 \\(toInterfaceOrientation)");  
}

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {  
    print("方向改变完成 ");  

/\* 在这里更新 约束 和 frame \*/

    UIView.animate(withDuration: deviceChangeOrientationTimeIntravel, animations: {  
        self.viewWillLayoutSubviews();  
        self.view.layoutIfNeeded();

    }) { (isFinish) in

        if isFinish == true{

        }  
    }  
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章