Home > Net >  How to bring a specifc view above all others?
How to bring a specifc view above all others?

Time:05-01

I have a circle and a sideView and want to simply put the sideView above my circle but I tried it with several commands like sideView.bringSubvieToFront(view) but that doesn't work. I checked as well the order of the views in storyboard to ensure that my sideView is at last position that doesn't help neither. It looks like the base View figures always above all other views which looks weird. Any simple way to bring this sideView on top of the circle?

@IBOutlet weak var sideView: UIView!

let shapeLayer = CAShapeLayer()


override func viewDidLoad() {
    super.viewDidLoad()
    
    sideView.bringSubviewToFront(view)

    let center = view.center
    
    let circularPath = UIBezierPath(arcCenter: center, radius: 125, startAngle: -CGFloat.pi / 2, endAngle: 2*CGFloat.pi - CGFloat.pi/2, clockwise: true)
    
    shapeLayer.path = circularPath.cgPath
    shapeLayer.lineWidth = 10
    //shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.fillColor = UIColor.red.cgColor
    shapeLayer.lineCap = CAShapeLayerLineCap.round
    view.layer.addSublayer(shapeLayer)
    shapeLayer.strokeColor = UIColor.green.cgColor
}

}

enter image description here

CodePudding user response:

You have two issues here.

  1. sideView.bringSubviewToFront(view)

Your sideView is a child of your controller's view, not the other way around. So you should call view.bringSubviewToFront(sideView). See documentation: the parameter is the subview.

  1. Code order

You're trying to move the subview to the front before you add other subviews and layers. The latter will ultimately be placed above. Move bringSubviewToFront after you've added other subviews.

  • Related