Ios view.addSubview(:)在swift游乐场中不工作
我有一个快速操场,我创建了一个叫做“开始按钮”的UIButton 我有一个设置UIButton参数的函数Ios view.addSubview(:)在swift游乐场中不工作,ios,swift,uiview,Ios,Swift,Uiview,我有一个快速操场,我创建了一个叫做“开始按钮”的UIButton 我有一个设置UIButton参数的函数 func setupStartButton() { startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50) startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, b
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonPressed), for: .touchUpInside)
view.addSubview(startButton)
}
我称之为viewDidLoad
override func viewDidLoad() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 524, height: 766))
view.backgroundColor = .white
setupStartButton()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
}
当我在操场上跑步时,它不会显示按钮。如何显示按钮。
谢谢您正在将
PlaygroundPage.current.liveView
设置为在viewDidLoad
中创建的本地视图
。这与在设置开始按钮中添加按钮的视图控制器视图不同
我将删除行let view=UIView(帧:CGRect(x:0,y:0,宽度:524,高度:766))
然后对视图的每个引用都将是视图控制器的视图
您应该从viewDidLoad
方法中删除游乐场代码。在视图控制器类之后,可以创建视图控制器的实例并设置游乐场:
比如:
class MyViewController: UIViewController {
// your code
}
PlaygroundPage.current.liveView = MyViewController()
基本上,操场不是这样的好地方。使用一个真正的应用程序项目。我正在为WWDC奖学金项目创建一个项目,遗憾的是,这个项目必须在操场上。顺便说一句,没有必要创建一个CIColor
,只需要从它创建一个UIColor
。您可以直接创建UIColor
。