Add UIView and UILabel programmatically on Mac OS

Viewed 244

I am trying to add a UIView then a UILabel as a subview on Mac OS and iPad. if I call the following function in loadView() and run the iPad simulator the background turns white and my UILabel appears in the middle where its supposed to. If I run the Mac OS simulator, the background turns white but I have no text. The app is a UIKit app with the checkbox to run on Mac using Mac Catalyst checked.

Any ideas as to why its not showing my UILabel when I run on Mac OS Sim?

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)
    ])
}
1 Answers

Have you looked at this in the view debugger? My guess is that the default color of UILabel is white. Try setting textColor to either an explicit color or .label. You might also want to use .systemBackground for the background color to automatically support dark mode.

Related