Xcode 8 操场实时视图不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43020859/
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:51:32  来源:igfitidea点击:

Xcode 8 playground live view doesn't work

iosswiftxcodeswift-playground

提问by Blip

In my Swift playground, I entered the following code to test the live view feature:

在我的 Swift Playground 中,我输入了以下代码来测试实时取景功能:

let view = UIView()

PlaygroundPage.current.liveView = view

But for some reason the live view doesn't display on the right in the Assistant Editor. Initially I thought that Xcode wasn't finished running the playground. But I waited and waited and it still doesn't show.

但由于某种原因,实时视图不会显示在助理编辑器的右侧。最初我认为 Xcode 还没有完成操场的运行。但我等了又等,它仍然没有显示。

Screenshot: enter image description here

截屏: 在此处输入图片说明

Any help?

有什么帮助吗?

回答by maxcodes

I had this same exact issue. I found a temporary solution.

我有同样的问题。我找到了一个临时解决方案。

What I noticed is that if I opened more than one Xcode project it would cause this error. So Just completely quit xcode (command Q) and only open the live playground you are trying to work on and it should work.

我注意到的是,如果我打开了多个 Xcode 项目,则会导致此错误。因此,只需完全退出 xcode(命令 Q)并仅打开您正在尝试处理的实时操场,它应该可以工作。

Make sure you have the following imports and you might want to give it a frame and color just to make sure it is actually working since your view does not have a frame or color. This code works for me.

确保你有以下导入,你可能想给它一个框架和颜色,以确保它实际工作,因为你的视图没有框架或颜色。这段代码对我有用。

import UIKit
import PlaygroundSupport

let view = UIView()
view.backgroundColor = .white
view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)

PlaygroundPage.current.liveView = view

playground live view

操场直播

回答by ReaddyEddy

In Xcode 9.0 as well as having:

在 Xcode 9.0 以及具有:

PlaygroundPage.current.needsIndefiniteExecution = true

You have to manually open the assistant editor then the playground with current focus will show the UIView.

您必须手动打开辅助编辑器,然后当前焦点的操场将显示 UIView。

回答by jacob

I had the same issue as well and the following line of code fixed it for me:

我也遇到了同样的问题,以下代码行为我修复了它:

PlaygroundPage.current.needsIndefiniteExecution = true

Setting a live view is supposed to auto-enable it for you, but for me it wasn't doing it. Must be a bug in Xcode.

设置实时视图应该为您自动启用它,但对我来说它没有这样做。一定是 Xcode 中的错误。

回答by ViktorCode

Setting a UIView as a live view only worked for me on iPad playgrounds but not in Xcode.

将 UIView 设置为实时视图仅适用于 iPad 游乐场,但不适用于 Xcode。

To make the view appear in Xcode 9 & 10 playground I wrapped it into a view controller and set that to live view instead (just like the default single view playground does):

为了让视图出现在 Xcode 9 & 10 playground 中,我将它包装到一个视图控制器中,并将其设置为实时视图(就像默认的单视图游乐场一样):

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        self.view = view
    }
}

PlaygroundPage.current.liveView = MyViewController()

回答by Konstantin Berkov

I was able to preview viewcontroller's view only explicitly leaving controller's view in playground code like this:

我能够预览视图控制器的视图,只是在操场代码中显式地离开控制器的视图,如下所示:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    // ...
}

let controller = MyViewController()
PlaygroundPage.current.liveView = controller
controller.view // <- to the far right of this line in editor you'll be able to expand and see result