如何在Swift Playground中添加一个UIButton?

4

我打开了playground,只是想添加一个简单的UIButton(或一个简单的UIView)进行测试。但是它无法显示。目前我的代码如下:

import UIKit

var uiButton    = UIButton.buttonWithType(UIButtonType.System) as UIButton
uiButton.frame  = CGRectMake(0, 0, 100, 100)
uiButton.setTitle("Test", forState: UIControlState.Normal);
//self.view.addSubview(uiButton) <-- doesn't work

你们知道我做错了什么吗?谢谢。

现在使用Xcode 7.3可以实现,详见此处答案... https://dev59.com/YmAf5IYBdhLWcg3w9Wmu#36228746 - MoFlo
4个回答

4

我认为你可以在Playground中添加按钮,你的代码是正确的。当你点击“Quick Look”时,你可以在这里看到你的按钮:

enter image description here

或者你可以通过点击“Value History”来查看该按钮:

enter image description here

你不需要将其添加到视图中。


2
如果您在您的Playground中导入XCPlayground,您可以使用以下方式显示视图:
let view=UIView()
//other setup
XCPShowView("View Title",view)

这将在助理编辑器中显示视图

要显示助理编辑器,请转到视图 > 助理编辑器 > 显示助理编辑器


2
在Xcode 7.1中已经弃用了这种方式;新方法是 let view = UIView XCPlaygroundPage.currentPage.liveView = view. - gemmakbarlow
请参阅 https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/XCPlaygroundModuleRef/XCPlayground.html 以获取更多详细信息。 - gemmakbarlow

0
I was just doing my sorting programming and pated the code..it will help you to add a button in playgroud with target.

import UIKit
import PlaygroundSupport

let aButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
aButton.backgroundColor = .lightGray
aButton.setTitle("Run Sorting", for: .normal)
aButton.layer.cornerRadius = 10
aButton.clipsToBounds = true

class Sorting {
    
    @objc func callSorting() {
        self.getSortedArrayUsingSelectionSort()
    }
    
    func getSortedArrayUsingSelectionSort() {
        
        var arr = [4, 9, 10, 6, 1, 3]
        let n = arr.count
        
        for i in 0..<(n - 1) {
            var minIndex = i
            for j in (i+1)..<n {
                if arr[j] < arr[minIndex] {
                    minIndex = j
                }
            }
            if minIndex != i {
                let temp = arr[minIndex]
                arr[minIndex] = arr[i]
                arr[i] = temp
            }        }
        print(arr)
    }
  }

let target = Sorting()

aButton.addTarget(target, action: #selector(Sorting.callSorting), for: .touchUpInside)
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
containerView.backgroundColor = .white
PlaygroundPage.current.liveView = containerView

containerView.addSubview(aButton)

0

import XCPlayground已被弃用,现在使用import PlaygroundSupport。 您需要先创建视图并为其指定大小。

let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) view.backgroundColor = UIColor.black

然后添加PlaygroundPage.current.liveView = view以在助理编辑器中查看视图。

现在您已经创建了视图,并且可以看到它的实时预览。您现在可以向其添加其他视图。在我的playground中,我添加了几个视图,因此我创建了一个带有默认宽度和高度的简单函数。

func addSquareView(x: Int, y: Int, width: Int = 100, height: Int = 100, color: UIColor) { let newView = UIView(frame: CGRect(x: x, y: y, width: width, height: height)) newView.backgroundColor = color view.addSubview(newView) }

addSquareView(x: 100, y: 100, color: .blue)

现在,在我的视图中心有一个蓝色正方形。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接