【问题标题】:Add UIView and UILabel programmatically on Mac OS在 Mac OS 上以编程方式添加 UIView 和 UILabel
【发布时间】: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)
    ])
}

【问题讨论】:

    标签: ios swift macos uikit


    【解决方案1】:

    您在视图调试器中查看过这个吗?我的猜测是 UILabel 的默认颜色是白色。尝试将textColor 设置为显式颜色或.label。您可能还想使用.systemBackground 作为背景颜色以自动支持深色模式。

    【讨论】:

    • 谢谢!我今天早上醒来时心想我敢打赌文字颜色是白色?‍♂️!我打开这个来找到你的答案?!我将背景更改为 systemBackground 并且效果很好。再次感谢
    猜你喜欢
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2013-04-08
    • 1970-01-01
    相关资源
    最近更新 更多