iOS 游乐场不显示 UI 预览

iOS Playground doesn't show UI preview

我用 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) 

playground 运行时,不显示 UIKit 对象预览,只显示调试信息:

我做错了什么?

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

然后在你的代码中:

import PlaygroundSupport
PlaygroundPage.current.liveView = view

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


Ps: Xcode 9后,可以创建默认视图的playground

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

这是 Xcode 8 的更新答案:

有几个步骤:

您需要导入 PlaygroundSupport

import PlaygroundSupport

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

PlaygroundPage.current.needsIndefiniteExecution = true

您需要将要显示的视图层级添加到liveView:

PlaygroundPage.current.liveView = container

整个游乐场可能是这样的:

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

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

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

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

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)

对于那些遇到助理编辑什么都不做的问题(对我来说它只是一片空白)的人来说,这是我解决问题的方法。

  1. 使用该代码进行测试:(上面接受的答案)

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

后者会触发Xcode重绘视图

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

只需选中游乐场设置下的显示时间轴即可。

在 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

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

在为单个视图创建 Xcode playground 并首次 运行 之后,不要忘记激活右上角的 Assistant Editor 以启用实时视图。

您可以转到编辑器 > Select 实时视图。在 XCode 11.5

中检查过

无需将 needsIndefiniteExecution 设置为 true,具有实时视图的 playground 页面会自动将其设置为 true