简体   繁体   中英

Override screen size on simulator device

I am wondering if it possible to override screen size in a setting and reload your app.

I want to do this so that if I have an iPhone X open, for example, I am able to open a debug menu on my app and default override the screen size, reload the app, and have it be testable as the size of an iPhone 6.

I want to do this so that I can test all different screen dimensions without compiling and running the app on all different simulators.

Thanks in advance!

This isn't possible. You need to build and test by running on each differently sized simulator that you wish to test.

What you are asking cannot be done that way, only you can create new window with different frame but to use in another device's screen. But you can change you current view controllers view frames according calculating the aspect ratio of each device and changing the height of you view according to that, or you can make the rect with the local dimensions too.

Make extension use it everywhere:

eg:

extension UIViewController {
   /* With exact value */
   func convertTo3_5inch() {
       // 320 × 480
       if UIScreen.main.bounds.height >= 480 {
           self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
           self.view.layoutIfNeeded()
       }
   }
   /* With Ratio */
   func convertTo16_9ratio() {
       // 320 * 568
       if UIScreen.main.bounds.height >= 568 {
           self.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.width*16)/9)
           self.view.layoutIfNeeded()
       } else {
           print("Screen size smaller than the size your are converting to")
       }

   }
   /* call this func in button */
   func changeAction() {
       let action = UIAlertController(title: "Options", message: nil, preferredStyle: .actionSheet)
       action.addAction(UIAlertAction(title: "3.5inch", style: .default, handler: { (action) in
           self.convertTo3_5inch()
    }))

       action.addAction(UIAlertAction(title: "5to8plus", style: .default, handler: { (action) in
           self.convertTo16_9ratio()
    }))

       self.present(action, animated: true, completion: .none)
   }
}

Customize according to your need,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM