G
G
Gleb2020-08-01 22:21:41
iOS
Gleb, 2020-08-01 22:21:41

How to make multiple backgrounds in Xcode?

According to the tutorial, I made an application for the weather and wanted to change the background after that, so that for each city from let cities = [...]
there was its own picture.
in View I see the opportunity to make a background once, how can I add several?
and after the following question, where to prescribe the condition for the appearance of the desired picture? viewDidLoad?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-08-02
@shevzoom

You need to break the task into subtasks . Let's formulate:

  1. How to set background color before showing controller
  2. How to set background for controller
  3. How to change the background of a controller
  4. How to change the background on an event

Now it became clear that your question consists of several, which means it would be nice to break them down. But as they wrote, so they wrote. Let's dive in.
How to set background color before showing controller?
Default values ​​are set in the viewDidLoad method . You rule suspected in question.
How to set background for controller?
You can set the background to the root view. But I would suggest a variant with UIImageView stretched to the edges of the parent and setting image. This can in some sense encapsulate the background logic and replace the class with something more interesting/complex in the future.
How to change the background of a controller?
To change the background, you need to have access to the image container. For example, you made the backgroundImageView property, then changing the image is not difficult:
class ViewController: UIViewController {
   var backgroundImageView = UIImageView()

   func viewDidLoad() {
      super.viewDidLoad()
      backgroundImageView.image = UIImage()
   }

   func changeBackground() {
      // Пример смены фона по вызову функции. Объект UIImage должен быть ваш, для примера пустой.
      backgroundImageView.image = UIImage()
   }
}

How to change the background on an event?
You need to define the event itself. If the button - then by target. Writing to the database - Observer. Perhaps system notifications will do, but you should be careful with them. In any case, for any of the events you will call a function, and in this function it will be enough to call the code from the previous question.
PS Your question is very trivial, and to be honest, that's why it has no answers . You should learn the basics , and then move on to practical tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question