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
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With