使用Playground在模拟器中闪烁UIView

时间:2015-08-13 09:46:24

标签: ios swift uiview swift-playground

我正在运行Xcode 6.4,在游乐场玩,我写了这个超级基本代码。

我想在模拟器中运行代码,所以我打开了实用工具选项卡(alt-cmd-0) 并选中支票簿“[v]在完全模拟器中运行”

import UIKit
import XCPlayground

let eenView = UIView(frame: CGRectMake(0, 0, 100, 100))
eenView.backgroundColor = UIColor.orangeColor()

XCPShowView("View", eenView)

模拟器正在显示,还有视图,但是......它正在闪烁!!

(我尝试了很多东西,比如重置所有的模拟器,创建新的模拟器,似乎没什么用,我想知道我是否需要在我的代码中添加一些内容)

1 个答案:

答案 0 :(得分:3)

<强>更新

import UIKit

//setup

struct MainScene {
    let vc: UIViewController
    let nc: UINavigationController
    init(vc: UIViewController) {
        self.vc = vc
        self.nc = UINavigationController(rootViewController: vc)
    }
}

extension UIViewController {
    class func viewController(color: UIColor) -> UIViewController {
        let vc = UIViewController()
        vc.view = UIView(frame: UIScreen.mainScreen().bounds)
        vc.view.backgroundColor = color
        return vc
    }
}

let vc = UIViewController.viewController(UIColor.lightGrayColor())
vc.title = "title"

////////Write your prototype code there

////////End of your prototype

let mainScene = MainScene(vc: vc)

//Run playground
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.rootViewController = mainScene.nc
window.makeKeyAndVisible()
CFRunLoopRun()

这是解决方案,如果有人感兴趣的话......

我修改了ilyapuchka的代码(他意识到非常好的例子):

https://gist.github.com/ilyapuchka/1ae19259161a91f3a8a8