Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift playground UITextField spawns keyboard that is too big

Swift in playground on Mac OS. When the user clicks in a UItextfield, a keyboard spawns but it is very large compared to the view and only the first few keys are available.

minimal example:

import UIKit
import PlaygroundSupport

class TesterViewController : UIViewController {
var testTextField : UITextField!
override func loadView() {
    let view = UIView()
    view.backgroundColor = .white

    testTextField = UITextField()
    testTextField.borderStyle = .roundedRect
    testTextField.text = ""
    view.addSubview(testTextField)
    testTextField.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        testTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
        testTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
        ])

    self.view = view
    }
}
PlaygroundPage.current.liveView = TesterViewController()

screenshot enter image description here

like image 283
David Gerhard Avatar asked Oct 21 '17 20:10

David Gerhard


People also ask

How do you stop an infinite loop in Swift playground?

While the playground is running, the source code is still editable. Just change the loop, type in a command, for example break into the code, inside the loop. This is interpreted code not compiled code so it will take effect.

How do I stop swift playground from running?

Apparently, on the lower panel, there's a play and stop button there as per picture below.

How do I export a swift playground?

You can export a playground book to share it with others or save a version for yourself. In the Swift Playgrounds app on your Mac, open a playground, then choose File > Export as New Playground.


2 Answers

System keyboard is presented according to the key window size and in the case of a playground simulator the size is 768x1024. It looks like it is a bug.

A solution which works:

Instead of passing a view controller one should pass a window with a custom size.

let window = UIWindow(frame: CGRect(x: 0,
                                    y: 0,
                                    width: 768,
                                    height: 1024))
let viewController = MyViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()

PlaygroundPage.current.liveView = window

Works in Xcode 11 and above (latest tested version Xcode 12.2)

Edited

I have checked the answer which suggests to use "vc.preferredContentSize" but it doesn't work for me. Tested in Xcode 12.2

let vc = TextFieldViewController()
vc.preferredContentSize = CGSize(width: 768,height: 1024)
PlaygroundPage.current.liveView = vc
like image 158
Blazej SLEBODA Avatar answered Oct 05 '22 00:10

Blazej SLEBODA


I face the same issue. It seems as if Playground has a hard-coded screen size of 768x1024 (run UIScreen.main.bounds in the Playground) and shows the keyboard according to this size, independently of the live view's actual size.

The best workaround I came up with is to increase the size of the view controller so that it matches the keyboard:

let vc = TesterViewController()
vc.preferredContentSize = vc.view.frame.size // or a custom CGSize
PlaygroundPage.current.liveView = vc

Of course this makes the view larger than you might want it to be, so I only use this workaround when I really have to access the on-screen keyboard for testing.

like image 27
dr_barto Avatar answered Oct 05 '22 00:10

dr_barto