ios - 将 UILabel 添加到子视图后无法显示
问题描述
@IBOutlet weak var result: UILabel!
@IBOutlet weak var testview: UIView!
override func viewDidLoad() {
super.viewDidLoad()
testview.layer.shadowColor = UIColor.systemGray.cgColor
testview.layer.shadowOpacity = 0.6
testview.layer.shadowOffset = .zero
testview.layer.shadowRadius = 5
testview.layer.shadowPath = UIBezierPath(rect: testview.bounds).cgPath
testview.layer.shouldRasterize = true
testview.layer.rasterizationScale = UIScreen.main.scale
testview.addSubview(result)
}
在我添加result
为testview
子视图后,它就消失了。
解决方案
您的 Storyboard 已经添加result
到视图层次结构中,因此当您再次添加它时,框架保持不变,但参考点会发生变化,因为现在超级视图是testView
,所以在您的情况下,它超出了可见范围。如果您希望UILabel
成为 的子视图,testView
我建议直接在 Storyboard 上进行更改,以编程方式创建 UILabel 而不是使用 Storyboards/IBOutlets 或在将标签添加到testView
.
推荐阅读
- ruby-on-rails - Papertrail:日志中的 whodunnit 值和 object_changes 为零的记录
- javascript - 如何在javascript中更改数组元素的属性?
- c# - 无法加载文件或程序集'System.Threading.Tasks.Extensions,版本 = 4.2.0.1
- javascript - 如何在对象数组中查找和比较单独的对象值
- python - 在 sympy 中删除双曲线图中的渐近线
- javascript - 如何在我的 Apexcharts 实际加载之前将“正在加载...”文本显示为占位符?
- c# - 选中复选框后,如何使对象可见?
- azure - Azure Active Directory SCIM:取消配置组的成员不起作用
- dynamics-crm - 将价值从产品传递到价目表项 crm2015
- rust - 使用 get_wch() 时的 ncurses-rs 段错误