-2

I am trying to set the background color of UIView programmatically. The code I am using is

UIView.appearance().backgroundColor = .blue

It works fine when I open the app, but as soon as I interact with it, the entire screen becomes a block of the selected color:what I see

  • 2
    The problem is that `UIView.appearance().backgroundColor = .blue` makes _every_ new view blue, forever after. Every view. Buttons. Labels. Everything. So yes, it is like spraying blue all over the interface. – matt Nov 29 '20 at 16:01

1 Answers1

0

first of all you have to give this view an outlet for example:

@IBOutlet weak var myView: UIView!

Then you can use this:

myView.backgroundColor = UIColor.blue

but the previous should be written in your

viewDidLoad()

so finally your code will be like that:

@IBOutlet weak var myView: UIView!

    override func viewDidLoad() {
         super.viewDidLoad()
         myView.backgroundColor = UIColor.blue
}
Menaim
  • 937
  • 7
  • 28