Does swift playground support UIKit?

前端 未结 10 1163
花落未央
花落未央 2020-12-07 23:54

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

相关标签:
10条回答
  • 2020-12-08 00:38

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

    0 讨论(0)
  • 2020-12-08 00:40

    Most simple solution: Xcode 9.2

    • Start with a new Single View playground:

    • 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

    0 讨论(0)
  • 2020-12-08 00:42

    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!"
    }
    
    0 讨论(0)
  • 2020-12-08 00:48

    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

    0 讨论(0)
提交回复
热议问题