Search code examples
swiftpositionuilabel

Send UILabel behind another UILabel (Swift)


I have a two overlapping UILabels onscreen and I wish to send the background UILabel in front of the front UILabel. How do I Do this? Is there a method of tagging them so that they will only move infant of each other as there are other UILabels and components on the screen also. Thanks.


Solution

  • A more comprehensive approach to this that gets exactly what you want is to extract the views array of your View and re-arrange it so that the view you care about is just 1 level higher:

    let superview = UIView() //your view (probably self.view)
    let labelToMove = UIView().removeFromSuperview() //label to move, do this so it can be re-inserted
    let labelToStay = //your label
    
    superview.insertSubview(labelToMove, aboveSubview: labelToStay)