swift 屏幕的翻轉 + 狀態欄(statusBar)的隱藏


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 = 1;
    case landscape = 2
}

// 定義常量
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 / 2))
                self.view.bounds = CGRect(x:0,y:0,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(0))
                self.view.bounds = CGRect(x:0,y:0,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{ } } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM