xcode 如何在 Playgrounds 中设置 ViewController?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43160615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:52:33  来源:igfitidea点击:

How to setup ViewController in Playgrounds?

swiftxcodeuiviewcontrollerswift-playground

提问by David Williamson

In swift playgrounds how do you setup a viewcontroller and use it? I've done it with a UIView before but the UI gets cut when in a difference orientation so I want to try use a viewcontroller. I've got let view = UIViewController()but after that how do I set it up to add a background and stuff on it?

在 swift playgrounds 如何设置视图控制器并使用它?我以前用 UIView 完成过,但是在不同方向时 UI 会被剪切,所以我想尝试使用视图控制器。我有,let view = UIViewController()但在那之后我如何设置它以添加背景和内容?

回答by David Williamson

I found that to create a viewcontroller in swift playgrounds you need this code

我发现要在 swift 游乐场中创建一个视图控制器,您需要此代码

import UIKit
import PlaygroundSupport

class ViewController:UIViewController{
    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

let viewController = ViewController()
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true

回答by Rizwan Mehboob

Example of UIViewController in Playground with multiple Subviews and Auto layout.

具有多个子视图和自动布局的 Playground 中的 UIViewController 示例。

import UIKit
import PlaygroundSupport

class ViewController : UIViewController {

    var yellowView: UIView!
    var redView: UIView!

    override func loadView() {

        // UI

        let view = UIView()
        view.backgroundColor = .white

        yellowView = UIView()
        yellowView.backgroundColor = .yellow
        view.addSubview(yellowView)

        redView = UIView()
        redView.backgroundColor = .red
        view.addSubview(redView)

        // Layout
        redView.translatesAutoresizingMaskIntoConstraints = false
        yellowView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            yellowView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
            yellowView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            yellowView.widthAnchor.constraint(equalToConstant: 80),
            yellowView.heightAnchor.constraint(equalToConstant: 80),

            redView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20),
            redView.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: -20),
            redView.widthAnchor.constraint(equalToConstant: 80),
            redView.heightAnchor.constraint(equalToConstant: 80)
            ])

        self.view = view
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

PlaygroundPage.current.liveView = ViewController()

enter image description here

在此处输入图片说明