【问题标题】:How to add a UIButton to Swift Playground?如何将 UIButton 添加到 Swift Playground?
【发布时间】:2014-12-24 03:37:26
【问题描述】:

所以我打开了 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

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

【问题讨论】:

标签: swift swift-playground


【解决方案1】:

我认为您可以在 Playground 中添加按钮,并且您的代码是正确的,当您单击“快速查看”时,您可以在此处看到您的按钮:

或者您可以通过单击“值历史记录”来查看该按钮:

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

【讨论】:

    【解决方案2】:

    如果您在 Playground 中 import XCPlayground,您可以使用以下方式显示视图:

    let view=UIView()
    //other setup
    XCPShowView("View Title",view)
    

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

    要显示助理编辑转到View &gt; Assistant Editor &gt; Show Assistant Editor

    【讨论】:

    【解决方案3】:

    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 以便能够在助手编辑器中看到视图。

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

    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)

    现在我的视野中心有一个蓝色方块。

    【讨论】:

      【解决方案4】:
      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)
      

      【讨论】:

        猜你喜欢
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-24
        相关资源
        最近更新 更多