我们只需要将想要显示的 UIView 子类赋值给当前 PlaygroundPage
的 liveView
属性,并且打开 Assistant Editor (Alt + Shift + Command + Return)
,就能看到我们创建的视图了
import UIKit
import PlaygroundSupport
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.backgroundColor = .cyan
label.font = UIFont.systemFont(ofSize: 35)
label.textAlignment = .center
label.text = "Hello World"
label.layer.masksToBounds = true
label.layer.cornerRadius = 20
PlaygroundPage.current.liveView = label
image.png
----------
import UIKit
import PlaygroundSupport
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .cyan
}
}
extension ViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 30
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = String(indexPath.row)
return cell
}
}
extension ViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Select: \(indexPath.row)")
}
}
let vc = ViewController()
PlaygroundPage.current.liveView = vc
网友评论