0

I have a problem in Swift where I put a UIImageView on my view in Main.storyboard. I want to use this UIImageView as a background image and therefore I want it on the bottom of the "stack of components". Unfortunately this UIImageView seems to be on the top of everything and not showing my UIPickerView and UIButtons. If i resize the UIImageView you can see what i mean by looking at the picture I attached.

Does anyone know how to solve this problem and set the UIImageView on the bottom, and all my other components above it?

The purple image is the one I want to use as background image

Answer: Thanks everyone, I fixed it by changing the backgroundview of the collectionview to the backgroundview. Here is the code:

    var imageViewObject :UIImageView!
    imageViewObject = UIImageView(frame: CGRectMake(0, 0, viewSize.width, viewSize.height))
    imageViewObject.image = UIImage(named:"intro.jpg")

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(NameCell.self, forCellWithReuseIdentifier: "Cell")
collectionView!.backgroundView = imageViewObject
backgroundView.addSubview(collectionView!)
user2099024
  • 1,452
  • 4
  • 16
  • 26
  • 1
    If you want it at the back, move it to the back. The layering order is totally up to you. What's the hard part here? – matt Dec 09 '14 at 20:49

3 Answers3

7

IF in storyboard:

  • click/select on the image you want to z-order
  • then from the menu bar
  • select Editor->Arrange
  • then choose "send to back" or "send to front"
  • optionally you can "send forward" or "send backward" to position between layers
ziligy
  • 1,447
  • 16
  • 10
3

Does anyone know how to solve this problem and set the UIImageView on the bottom, and all my other components above it?

Three things:

  1. You can set the z-order of your views programmatically. Take a look at -sendSubviewToBack:, -insertSubview:aboveSubview:, etc.

  2. If you need to do the same in your storyboard, just reorder the views using the commands in the Arrange submenu of the Editor menu in the storyboard editor.

  3. It's often better to have views contain other views. Consider adding the "front" views as subviews of the background view. That way you can move the whole "screen" by moving the background view, and the subviews will go along with it.

Caleb
  • 124,013
  • 19
  • 183
  • 272
1

Your question inspired me to write this blog post to show how to do it with both Storyboards and inside code, hopefully this will work for you. UIView Hierarchy

darren102
  • 2,830
  • 1
  • 22
  • 24