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

- 7,983
- 3
- 57
- 80

- 1,604
- 2
- 14
- 30
10 Answers
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.
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.
(EDIT: minor text update to current CGRect syntax -- But, screenshots still show old syntax.)

- 5,703
- 1
- 35
- 30
-
3MechEthan, I followed your instruction to create the playgroud and just type the code you provided, but the playgroud can't even find UILabel after I import UIKit. I have to import UIKit.UILabel, and then it tips can't resolved CGRectMake ...... Then I try to create a project and add a new playgroud into project. Xcode tips 'Mach error 111 - unknown error code' again and again when i'm typing, I can't even type the code! My Xcode Version 6.0 (6A215l). – 6david9 Jun 09 '14 at 10:59
-
@6david9 Weird, I can't reproduce your errors. I just created a new iOS project with Swift as language, added a new Playground file and typed in my UILabel code, no errors / issues. I did have to do a "Build" to get the 'UILabel' to appear in the right tray. Xcode 6.0 (6A215l) -- try rebooting maybe?? I'm at a loss! – MechEthan Jun 10 '14 at 20:34
-
14A playground is assigned to one platform at a time - OS X or iOS. If you can't import UIKit you are probably on OS X. See @6david9 's answer for instructions on how to change platforms. – Drew C Jun 30 '14 at 16:37
-
Awesome answer, +1 for the Marauder / Glaug. – n_b Aug 18 '16 at 04:01
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:
- View -> Utilities -> Show File Inspector (opt + cmd + 1)
- 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

- 786
- 5
- 16
-
1David, while it's always worth filing a bug when things don't behave the way you expect, the release notes for the beta specify that some UIKit elements that use advanced facilities (OpenGL for instance) to render themselves, won't display correctly in an iOS playground. OSX playgrounds should not suffer from the same issue. But, please, file a bug anyway. – Enrico Granata Jun 04 '14 at 17:01
-
3
-
I don't if anything worng with me or my Xcode, When I try the stpe 1. (View -> Utilities -> Show File Inspector (opt + cmd + 1). Nothing happens for me. :( – iOS_Developer Oct 08 '15 at 00:17
-
1. Do you using Playground? 2. if so click the top right button on the playground window, choose file inspector( or opt + cmd+ 1). @iOS_Developer – 6david9 Oct 10 '15 at 07:43
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!"
}
In Xcode 8 XCPlaygroundPage.currentPage.liveView
is deprecated. Instead, use
import PlaygroundSupport
PlaygroundPage.current.liveView = view

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

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

- 1,604
- 2
- 14
- 30
please use Command (⌘) + Option(⌥) + 1
combination to switch to iOS platform from OSX in playground to use UIKit .

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

- 4,591
- 1
- 25
- 31
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.

- 1,174
- 1
- 9
- 20
-
@MJN, I'd let the user decide on their own answer. Vote if you don't agree. – James Webster Jun 03 '14 at 08:04