Search code examples
xcodescreen-sizeui-testingiphone-se

Is there a way to simulate a smaller iPhone on my Plus?


I have an iPhone Plus which I'm using for development. I'm concerned that my app would be cramped or otherwise not have a good UI on 4-inch phones like the SE. Is there a way to simulate a 4-inch phone on the Plus? I imagine it would leave blank space at the top and sides of the screen.

The simulator doesn't work, since the scale is wrong. Everything appears bigger. It's also hard to test touch gestures on the simulator to see if they feel natural. Finally, it's a camera app, so it doesn't run on the simulator.

I'm fine with a third-party app or library to do this.


Solution

  • You can easily simulate this using a container view controller:

    class FourInchViewController: UIViewController {
    
      init(child: UIViewController) {
        self.child = child
        super.init(nibName: nil, bundle: nil)
    
        addChildViewController(child)
      }
    
    
    
    
      // Internals:
    
      private let child: UIViewController
    
      override func loadView() {
        let black = UIView()
        black.backgroundColor = .black
        view = black
    
        view.addSubview(child.view)
      }
    
      override func viewWillLayoutSubviews() {
        let size = view.bounds.size
        let w = CGFloat(320), h = CGFloat(568)
        child.view.frame = CGRect(
            x:(size.width - w)/2,
            y:size.height - h,
            width: w,
            height: h)
      }
    
      required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }
    }
    

    Then, in your app delegate, set this as your root view controller.