iOS Playground 不显示 UI 预览

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

iOS Playground doesn't show UI preview

iosswiftxcodeuikitswift-playground

提问by Lubbo

I've created a simple playground with XCode 7.1 and I've typed this simple code:

我用 XCode 7.1 创建了一个简单的操场,我输入了这个简单的代码:

import UIKit 
import XCPlayground 

var str = "Hello, playground" 

let color = UIColor (red: 1 , green: 1 , blue: 0 , alpha: 0 ) 

let view = UIView() 
view.backgroundColor = UIColo (colorLiteralRed: 1 , green: 0 , blue: 0 , alpha: 0 ) 

view.frame = CGRect (x: 0 ,y: 0 ,width: 100 ,height: 100 ) 

let label = UILabel (frame: CGRect (x: 5 , y: 5 , width: 50 , height: 20 )) 

label.text = str 

view.addSubview(label) 

When the playground runs, it doesn't show UIKit object preview, but only debug information:

当 Playground 运行时,它不显示 UIKit 对象预览,而只显示调试信息:

playground

操场

What am I doing wrong?

我究竟做错了什么?

回答by duan

Open the preview panel: View > Assistant Editor > Show Assistant Editor

打开预览面板:查看 > 助理编辑器 > 显示助理编辑器

Then in your code:

然后在你的代码中:

import PlaygroundSupport
PlaygroundPage.current.liveView = view

Don't forget to give your view a visible frame.

不要忘记给你的视图一个可见的框架。



Ps: after Xcode 9, you can create a playground with default view

Ps:在Xcode 9之后,你可以用默认视图创建一个playground

playground

操场

回答by Duncan C

The accepted answer is a screen-shot, not in-line code, and it shows how to do this pre Xcode 8, so I don't think it's a good answer.

接受的答案是屏幕截图,而不是内嵌代码,它显示了如何在 Xcode 8 之前执行此操作,因此我认为这不是一个好的答案。

Here is an updated answer For Xcode 8:

这是 Xcode 8 的更新答案:

There are several steps:

有几个步骤:

You need to import PlaygroundSupport

您需要导入 PlaygroundSupport

import PlaygroundSupport

You need to set PlaygroundPage.current.needsIndefiniteExecution to true:

您需要将 PlaygroundPage.current.needsIndeterminExecution 设置为 true:

PlaygroundPage.current.needsIndefiniteExecution = true

You need to add the view hierarchy you want to display to the liveView:

您需要将要显示的视图层次结构添加到 liveView:

PlaygroundPage.current.liveView = container

The whole playground might look like this:

整个操场可能看起来像这样:

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
view.backgroundColor = UIColor.red
container.addSubview(view)

PlaygroundPage.current.liveView = container
PlaygroundPage.current.needsIndefiniteExecution = true

Finally, you need to see the live view you just created. On my copy of Xcode 8 (release version) If I try to select view menu>assistant editor>Show Assistant Editor Xcode crashes, every time.

最后,您需要查看刚刚创建的实时视图。在我的 Xcode 8(发行版)副本上,如果我尝试选择查看菜单 > 助理编辑器 > 显示助理编辑器,Xcode 每次都会崩溃。

I have to hover my mouse on the right side of the screen in the box to the right of the code, on the line where I create the container view. I see a little eye icon, and if I click on that, it shows the live view without crashing.

我必须将鼠标悬停在屏幕右侧代码右侧的框中,在我创建容器视图的行上。我看到一个小眼睛图标,如果我点击它,它会显示实时视图而不会崩溃。

回答by DevAndArtist

For those of you who experience the problem that the assistant editor doesn't do anything (for me it was simply blank), here is how I solved the problem.

对于那些遇到助理编辑不做任何事情(对我来说只是空白)的问题的人,这是我解决问题的方法。

  1. Use that code to test: https://stackoverflow.com/a/33543820/4572536(accepted answer from above)

  2. View -> Assistent Editor -> Assistent Editors on Bottom(or any other option)

  1. 使用该代码进行测试:https: //stackoverflow.com/a/33543820/4572536(从上面接受的答案)

  2. View -> Assistent Editor -> Assistent Editors on Bottom(或任何其他选项)

The latter will trigger Xcode to redraw the view.

后者将触发 Xcode 重绘视图。

Note: I'm sharing this as a possible workaround, because for me my Xcode didn't draw the view even after full system restarts. I have no idea how to reproduce that issue, but if you happen to bump into it you can try this workaround.

注意:我将此作为一种可能的解决方法进行分享,因为对我来说,即使在整个系统重新启动后,我的 Xcode 也没有绘制视图。我不知道如何重现该问题,但如果您碰巧遇到它,您可以尝试此解决方法。

回答by code4latte

Besides syntax, I found an other trick to bring live view back to display in my case(Xcode 8.2.1 Swift 3.0). Also this might a minor bug in Xcode. Just click the + button at the top of assistant editor. Because sometime your assistant editor is showing the interface file of your file and refuse to automatically show live page.

除了语法之外,我还发现了另一个让实时视图在我的案例中显示的技巧(Xcode 8.2.1 Swift 3.0)。这也可能是 Xcode 中的一个小错误。只需单击助理编辑器顶部的 + 按钮即可。因为有时候你的助理编辑器会显示你文件的界面文件,拒绝自动显示实时页面。

enter image description here

在此处输入图片说明

回答by Fiko

Just check Show Timeline under Playground Settings.

只需检查 Playground Settings 下的 Show Timeline。

回答by jglasse

I don't see why you need the container view the previous answer requires. This works just as well:

我不明白为什么您需要上一个答案所需的容器视图。这也同样有效:

import UIKit
import XCPlayground
import PlaygroundSupport
var str = "Hello, playground"
let color = UIColor (red: 1 , green: 1 , blue: 0 , alpha: 0 )
let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
view.backgroundColor = UIColor.red
view.frame = CGRect (x: 0 ,y: 0 ,width: 200 ,height: 200 )
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = view
let label = UILabel (frame: CGRect (x: 5 , y: 5 , width: 200 , height: 20 ))
label.text = str
view.addSubview(label)

回答by Viktor Malyi

After creating Xcode playground for a single view and running it for the first time, don't forget to activate Assistant Editor in the right upper corner to enable Live View. enter image description here

在为单个视图创建 Xcode playground 并首次运行后,不要忘记激活右上角的 Assistant Editor 以启用 Live View。 在此处输入图片说明

回答by smileBot

In Xcode 8.3 I'm still having issues. The only thing that works is shutting down Xcode and relaunching it. I tried quitting the simulator since I notice it is running the simulator, but this didn't fix the issue. Will file a radar.

在 Xcode 8.3 中,我仍然有问题。唯一有效的方法是关闭 Xcode 并重新启动它。我尝试退出模拟器,因为我注意到它正在运行模拟器,但这并没有解决问题。将提交雷达。

import UIKit

import PlaygroundSupport



let frame = CGRect(x: 0, y: 0, width: 300, height: 700)
let container = UIView(frame: frame)
container.backgroundColor = .red

let inner = UIView(frame: CGRect(x: 100, y: 100, width: 20, height: 40))

inner.backgroundColor = .blue
container.addSubview(inner)

PlaygroundPage.current.liveView = container
PlaygroundPage.current.needsIndefiniteExecution = true