【发布时间】:2020-01-05 12:57:51
【问题描述】:
我正在尝试显示已添加到视图中的子视图,但按下按钮时它不显示。
我尝试将 isOpaque 设置为 1,将 alpha 设置为 1,将 isHidden 设置为 false(无需按下按钮)并检查我是否运行了 view.addSubview()。我还发现子视图根本没有隐藏,但背景是白色的(应该是蓝色或红色)。
添加子视图的代码
//setup
viewBGKRect = CGRect(x: 20, y: 20, width: 984, height: 660)
viewBGK = UIView(frame: viewBGKRect)
viewBGK.backgroundColor = UIColor(red: 139.0, green: 206.0, blue: 231.0, alpha: 1.0)
viewBGK.alpha = 1
viewBGK.isOpaque = true
viewRGKRect = CGRect(x: 20, y: 20, width: 984, height: 660)
viewRGK = UIView(frame: viewRGKRect)
viewRGK.backgroundColor = UIColor(red: 240.0, green: 177.0, blue: 187.0, alpha: 1.0)
viewRGK.alpha = 1
viewRGK.isOpaque = true
//isHidden is set to false when the buttons are pressed
viewBGK.isHidden = true
viewRGK.isHidden = true
view.addSubview(viewBGK)
view.addSubview(viewRGK)
显示子视图的代码
@IBAction func goalkeeper(_ sender: UIButton) {
switch sender.tag {
case 0:
// blue
viewBGK.isHidden = false
viewRGK.isHidden = true
return
default:
viewBGK.isHidden = true
viewRGK.isHidden = false
return
}
}
我希望屏幕顶部会出现一个蓝色/红色矩形,但它没有显示。
【问题讨论】:
-
你把“添加子视图的代码”放在哪里了?
viewDidLoad?另外,什么视图没有显示?蓝色或红色或两者兼而有之?
标签: swift addsubview