Created a view, threw objects and decided to add a background image for the view. Posted by:

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background1.png"]]; [self.view addSubview:backgroundView]; 

As a result, the picture completely blocked all the controls. How do I send a drawing to the background?

    4 answers 4

    In general, any UIView consists of CGLayers and each UIVIew has a backgroundLayer, which takes a UIColor as input. But there is such a trick:

      [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background1.png"]]]; 

    If the picture is small, it will tile itself with repetitions of itself, but that is another question.

      insertSubview: atIndex: And so you insert on top of everything. And the question arises, if everything is not created dynamically, why should the background be made dynamically? Dynamic addition of components is very poorly perceived by other developers, who will then use your code, IMHO.

      • Thanks guys for the help, I already found out 3 options for adding the background :) Now another question: I added a picture and it blocked the status bar, only the battery pattern is visible. - michilly
      • one
        a, false alarm. repaired - michilly

      When you add a subview via [self.view addSubview:] your subview is added to the very top of the hierarchy, and therefore overlaps with the view you added earlier.

      To manage this process use methods.

       [self.view insertSubview:<#(nonnull UIView *)#> aboveSubview:<#(nonnull UIView *)#>] [self.view insertSubview:<#(nonnull UIView *)#> belowSubview:<#(nonnull UIView *)#>] 

      Thus, you can specify exactly where in the hierarchy to add your subview.

      In your case, do this:

       [self.view insertSubview:backgroundView aboveSubview:self.view] 
         self.view.layer.contents = (id)[UIImage imageNamed:@"background1.png"].CGImage;