How to send a UIView element back to Swift

So, I have a button that I added to main.Storyboard, as well as the main background for the view, and then in the viewController.swift file, I created the UIView rectangle shape that I want to sit behind this button (as the background for it) .

The problem is that when I added the UIView rectangle, it always added it before the button. So I searched online and found the sendSubviewToBack code. I added this, but it sends it completely behind the main background of the UIImage view.

Is there a way that I can just send this UIView behind the button, but before the UIImage background?

func nextBarDisplayed() { let theHeight = self.view.frame.height nextBar.backgroundColor = UIColor(red: 7/255, green: 152/255, blue: 253/255, alpha: 0.5) nextBar.frame = CGRect(x: 0, y: theHeight - 50, width: self.view.frame.width, height: 50) self.view.insertSubview(nextBar, aboveSubview: myBackgroundView) } 
+15
ios iphone swift uiview
source share
3 answers

From the documentation for Apple :

 bringSubviewToFront(_:) sendSubviewToBack(_:) removeFromSuperview() insertSubview(_:atIndex:) insertSubview(_:aboveSubview:) insertSubview(_:belowSubview:) exchangeSubviewAtIndex(_:withSubviewAtIndex:) 

You can bring the bar to the foreground using:

 view.bringSubviewToFront(nextBar) 

Submit your presentation on the back using:

 nextBar.view.sendSubviewToBack(myView) 
+36
source share

For Swift 4:

 self.view.sendSubview(toBack: myView) 
+3
source share

In Swift 4.2, this can be done with:

 self.sendSubviewToBack(view: viewSentToBack) 

An object in a view that is bound by an IBOutlet can be sent back using the above function.

0
source share

All Articles