【问题标题】:Override screen size on simulator device覆盖模拟器设备上的屏幕尺寸
【发布时间】:2018-06-20 14:36:00
【问题描述】:

我想知道是否可以在设置中覆盖屏幕大小并重新加载您的应用。

我想这样做,例如,如果我打开 iPhone X,我可以在我的应用程序上打开一个调试菜单并默认覆盖屏幕大小,重新加载应用程序,并让它作为可测试的iPhone 6 的大小。

我想这样做,以便我可以测试所有不同的屏幕尺寸,而无需在所有不同的模拟器上编译和运行应用程序。

提前致谢!

【问题讨论】:

  • Interface Builder 支持这种即时切换,但 Simulator 不支持。您必须切换模拟器目标才能切换设备。
  • 如果您只是想测试尺寸,而不是实际设备,您可以调整根视图控制器的视图大小,但我强烈反对这样做!

标签: ios autolayout uiscreen


【解决方案1】:

这是不可能的。您需要通过在您希望测试的每个不同大小的模拟器上运行来构建和测试。

【讨论】:

    【解决方案2】:

    您所要求的不能那样做,只有您可以创建具有不同框架的新窗口,但可以在另一个设备的屏幕上使用。但是您可以根据计算每个设备的纵横比并据此更改视图的高度来更改当前视图控制器的视图框架,或者您也可以使用本地尺寸制作矩形。

    让扩展在任何地方使用它:

    例如:

    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)
       }
    }
    

    根据您的需要定制,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多