iOS平台没有显示用户界面预览。

时间:2023-01-23 18:06:57

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:

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

iOS平台没有显示用户界面预览。

What am I doing wrong?

我做错了什么?

7 个解决方案

#1


120  

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: In Xcode 9, you can create a playground with default view

在Xcode 9中,你可以创建一个带有默认视图的运动场

iOS平台没有显示用户界面预览。

#2


29  

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.

公认的答案是屏幕截图,而不是内嵌代码,它展示了如何执行这个pre - Xcode 8,所以我认为这不是一个好的答案。

Here is an updated answer For 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。needsIndefiniteExecution真实:

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(发布版本)拷贝中,如果我尝试选择view菜单>assistant editor>显示assistant editor 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.

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

#3


8  

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://*.com/a/33543820/4572536 (accepted answer from above)

    使用该代码进行测试:https://*.com/a/33543820/4572536(接受以上答案)

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

    视图->辅助编辑器-底部的>辅助编辑器(或任何其他选项)

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在整个系统重新启动之后也没有绘制视图。我不知道如何重现这个问题,但如果你碰巧遇到它,你可以试试这个方法。

#4


2  

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)

#5


2  

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.

除了语法之外,我还发现了另一个技巧,可以在我的例子中将live view重新显示出来(Xcode 8.2.1 Swift 3.0)。这也可能是Xcode中的一个小错误。点击助理编辑器顶部的+按钮。因为有时候你的助理编辑会显示你的文件的接口文件并且拒绝自动显示实时页面。

iOS平台没有显示用户界面预览。

#6


1  

Just check Show Timeline under Playground Settings.

只要在操场设置下查看显示时间线。

iOS平台没有显示用户界面预览。

#7


0  

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

#1


120  

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: In Xcode 9, you can create a playground with default view

在Xcode 9中,你可以创建一个带有默认视图的运动场

iOS平台没有显示用户界面预览。

#2


29  

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.

公认的答案是屏幕截图,而不是内嵌代码,它展示了如何执行这个pre - Xcode 8,所以我认为这不是一个好的答案。

Here is an updated answer For 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。needsIndefiniteExecution真实:

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(发布版本)拷贝中,如果我尝试选择view菜单>assistant editor>显示assistant editor 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.

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

#3


8  

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://*.com/a/33543820/4572536 (accepted answer from above)

    使用该代码进行测试:https://*.com/a/33543820/4572536(接受以上答案)

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

    视图->辅助编辑器-底部的>辅助编辑器(或任何其他选项)

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在整个系统重新启动之后也没有绘制视图。我不知道如何重现这个问题,但如果你碰巧遇到它,你可以试试这个方法。

#4


2  

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)

#5


2  

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.

除了语法之外,我还发现了另一个技巧,可以在我的例子中将live view重新显示出来(Xcode 8.2.1 Swift 3.0)。这也可能是Xcode中的一个小错误。点击助理编辑器顶部的+按钮。因为有时候你的助理编辑会显示你的文件的接口文件并且拒绝自动显示实时页面。

iOS平台没有显示用户界面预览。

#6


1  

Just check Show Timeline under Playground Settings.

只要在操场设置下查看显示时间线。

iOS平台没有显示用户界面预览。

#7


0  

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