【发布时间】:2020-12-17 09:13:56
【问题描述】:
我试图在 Mac OS 和 iPad 上添加一个 UIView 然后一个 UILabel 作为子视图。如果我在 loadView() 中调用以下函数并运行 iPad 模拟器,则背景变为白色,并且我的 UILabel 出现在它应该出现的中间。如果我运行 Mac OS 模拟器,背景会变成白色,但我没有文字。该应用程序是一个 UIKit 应用程序,其中选中了使用 Mac Catalyst 在 Mac 上运行的复选框。
关于为什么我在 Mac OS Sim 上运行时它没有显示我的 UILabel 有什么想法吗?
func coreIdea() {
view = UIView()
coreIdeaLabel = UILabel()
view.backgroundColor = UIColor.white
coreIdeaLabel.translatesAutoresizingMaskIntoConstraints = false
coreIdeaLabel.textAlignment = .center
coreIdeaLabel.text = "CoreIdea"
coreIdeaLabel.font = UIFont.systemFont(ofSize: 24)
view.addSubview(coreIdeaLabel)
NSLayoutConstraint.activate([
coreIdeaLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
coreIdeaLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
【问题讨论】: