Does swift playground support UIKit?

UikitSwiftSwift Playground

Uikit Problem Overview


I tried to create a UILabel in playground but failed. Does playground only support OS X development for now?

Uikit Solutions


Solution 1 - Uikit

YES, it does!

File: New > File... > iOS > Source > Playground

import UIKit
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 300, height: 100))
lbl.text = "Hello StackOverflow!"

Then, save the file. (Or manually run it.) This will trigger the Playground to interpret UI related things. At this point, the word "UILabel" should appear on the right-hand side.

ios playground quickview

Now, to actually view what you've done, you've got to click on the "Quick View" eye on the right, or the white circle to open it in Assistant Editor:

Here's a screenshot of some basic things with UIImage working, etc. ios playground example

(EDIT: minor text update to current CGRect syntax -- But, screenshots still show old syntax.)

Solution 2 - Uikit

Edited@2014-11-13: It seems the new xcode 6 had fixed this.

NO, It doesn't. But it's worth noting that you can import UIKit.

If you want to import UIKit you cound follow this:

  1. View -> Utilities -> Show File Inspector (opt + cmd + 1)
  2. On the right side of Xcode Change “Playground Settings -> Platform” from OS X to iOS

then you could import UIKit or some module for iOS

ps. I try to create a UIImageView but it doesn't show the correct image on the right side. It seem worthless to import UIKit

Solution 3 - Uikit

In Xcode 7, now you can't use the Quick Look to see the appearance of a UIView.

Instead, use the Assistant Editor and:

XCPlaygroundPage.currentPage.liveView = sampleView

Like this:

import XCPlayground
import UIKit

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

// Simulate User Interaction, not available in Xcode 7.2
func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

let color = UIColor(red: 1, green: 1, blue: 0, alpha: 1)
let leftMargin = 20
let view = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667)) // iPhone 6 proportions
view.backgroundColor = UIColor.grayColor()

// LABEL
let label = UILabel(frame: CGRect(x: leftMargin, y: 5, width: 300, height: 44))
label.text = "Hello, playground"
label.textColor = UIColor.whiteColor()
view.addSubview(label)

// TEXTFIELD
let textField = UITextField(frame: CGRect(x: leftMargin, y: 60, width: 300, height: 44))
textField.placeholder = "Edit me…"
textField.backgroundColor = UIColor(white: 1, alpha: 0.5)
textField.textColor = UIColor.whiteColor()
textField.userInteractionEnabled = true
view.addSubview(textField)

XCPlaygroundPage.currentPage.liveView = view

delay(1.0) { () -> () in
    textField.text = "New text!"
}

Solution 4 - Uikit

In Xcode 8 XCPlaygroundPage.currentPage.liveView is deprecated. Instead, use

import PlaygroundSupport 

PlaygroundPage.current.liveView = view

Solution 5 - Uikit

Press CMD+Option+1 and change the platform to iOS, this will allow you to import UIKit.

Solution 6 - Uikit

I found I could add a new playground file in IOS project, and in that file I can import UIKit.

Solution 7 - Uikit

please use Command (⌘) + Option(⌥) + 1 combination to switch to iOS platform from OSX in playground to use UIKit .

Solution 8 - Uikit

Most simple solution: Xcode 9.2

  • Start with a new Single View playground:

enter image description here

  • show Assistance Editor: View -> Assistance Editor -> Show Assistance Editor.

  • here you go. By default you will see printed property: label.text = "Hello World!" in the Live View window

Solution 9 - Uikit

Press Option+Cmd+1 and choose iOS in the Platform setting. Then you can import UIKit and play~

Solution 10 - Uikit

Yeah looks like it doesn't support UIkit yet.

Update: When i wrote this answer in 2014 it didn't support UIKit, now it does. Leaving this for historical reference

Edit: Actually above answer is incorrect.

You can create iOS project and add new .playground file inside that project. Then you can import Uikit or another iOS specific framework.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbydskyView Question on Stackoverflow
Solution 1 - UikitMechEthanView Answer on Stackoverflow
Solution 2 - Uikit6david9View Answer on Stackoverflow
Solution 3 - UikitduanView Answer on Stackoverflow
Solution 4 - UikitEvgeny MikhaylovView Answer on Stackoverflow
Solution 5 - Uikituser3630017View Answer on Stackoverflow
Solution 6 - UikitbydskyView Answer on Stackoverflow
Solution 7 - UikitKeshav KumarView Answer on Stackoverflow
Solution 8 - UikitVolodymyrView Answer on Stackoverflow
Solution 9 - UikitonevcatView Answer on Stackoverflow
Solution 10 - UikitWarunaView Answer on Stackoverflow