Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Xcode Playground execute liveView through iPad-like Simulator?

enter image description here

I'm trying some codes in Xcode 8.2.1 Playground with Swift 3.
I've been confused since PlaygroundPage.current.liveView executes iPad-like simulator.
I want to test keyboard input through a simulator of smaller device. Can I handle this better?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupTextView()
    }

    private func setupTextView() {
        let textView = UITextView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
        textView.backgroundColor = .gray
        textView.isSelectable = true
        textView.font = textView.font?.withSize(20)
        view.addSubview(textView)
    }

}

let viewController = ViewController()
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 640, height: 600))
window.rootViewController = viewController
window.makeKeyAndVisible()

import PlaygroundSupport

PlaygroundPage.current.liveView = window
PlaygroundPage.current.needsIndefiniteExecution = true
like image 912
keisei_1092 Avatar asked Feb 25 '17 06:02

keisei_1092


People also ask

Does Swift playground work on iPad?

As your knowledge of Swift grows, you can begin learning SwiftUI, the framework you'll use to build apps right on your iPad. Explore the code in the tutorial app “Get Started with Apps” on the More Playgrounds screen and try adding some of your own code to see what it does.

How do I run a playground in Xcode?

To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

What is Xcode playground used for?

Xcode has a built-in feature called Playgrounds. It's an interactive development environment for developers to experiment Swift programming and allows you to see the result of your code in real-time.

What programming language is Swift Playgrounds?

Swift Playgrounds is an educational tool and development environment for the Swift programming language developed by Apple Inc., initially announced at the WWDC 2016 conference. It was introduced as an iPad application alongside iOS 10, with a macOS version introduced in February 2020.


1 Answers

Instead of

let viewController = ViewController()
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 640, height: 600))
window.rootViewController = viewController
window.makeKeyAndVisible()

import PlaygroundSupport

PlaygroundPage.current.liveView = window
PlaygroundPage.current.needsIndefiniteExecution = true

Try this

let viewController = ViewController()
viewController.preferredContentSize = CGSize(width: 640, height: 600)

import PlaygroundSupport

PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true

This is how I change the size of my view controllers in Playgrounds

like image 58
raulriera Avatar answered Oct 10 '22 02:10

raulriera