1

I have a method that draws pins by adding an ImageView on top of a slider when a button is pressed. Here is the code:

    var loopStartImageView : UIImageView = UIImageView()


    func addLoopDrawing(at time: CMTime, for loop: Int) {

        let imgHeight : CGFloat = 30
        let imgWidth: CGFloat = 30

        var pinImg : UIImage = UIImage(named: "pin2x")!

        let inset = slider.frame.origin.x
        let width = slider.bounds.width

        let xPos : CGFloat = CGFloat((Float(time.seconds) / slider.maximumValue)) * width + inset - (imgWidth / 2)
        var yPos : CGFloat = slider.frame.origin.y - (slider.frame.height / 2) - 3

        let imgV = UIImageView(frame: CGRect(x: xPos, y: yPos, width: imgWidth, height: imgHeight))
        imgV.image = pinImg

        loopStartImageView = imgV
        view.addSubview(loopStartImageView)
        view.sendSubviewToBack(loopStartImageView)
    }

The drawing is correct when I don't use autolayout to set the position of the slider, but once I do it shows up below the actual positioning.

This method is called in viewDidLoad. My guess is that for some reason the auto layout positioning is not set when viewDidLoad is called.

I was wondering if there is a method that is called once auto layout is fully adjusted?

//

This is what it should look like:

What it should look like

But this is how the view loads (even when calling the addLoopDrawing function inside the viewDidLayoutSubviews)

enter image description here

spitchay
  • 89
  • 1
  • 10
  • I think it would be easier to help if we could see what was your intended result. Could you include more details about this slider and/or what is being done with autolayout? – Michael Fourre Sep 17 '19 at 21:05
  • The images added in your edit are helpful here, thanks. Is there any reason that you can't do all of this with autolayout? Your results would probably be much more consistent that way. For example, you might create constraints in autolayout, connect them to the view controller with outlets, and then modify those constraints to reflect your desired pin position. – Michael Fourre Sep 17 '19 at 21:48
  • @MichaelFourre How would I go about adding the ImageViews with the pins with auto layout constraints programmatically? – spitchay Sep 17 '19 at 22:45
  • It seems as though the y-values of the slider incorrect when called in the viewDidLoad -- when I printed them, it said 558 in the viewDidLoad and 470 in the correct position – spitchay Sep 17 '19 at 23:20

1 Answers1

4

I was wondering if there is a method that is called once auto layout is fully adjusted?

That would be viewDidLayoutSubviews. That is a very good time to do things that depend upon things having their correct frame, but beware: it can be called many times. The usual thing, therefore, is to implement it along with a Bool property to check that this is the first time.

On the other hand, it might be better not to ask an x-y question like this. The idea of an image view on top of a slider seems wrong. It might be better to describe what you are really trying to do.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I would like to echo that the premise here seems wrong. It sounds like OP may be going about this in a way that could cause more problems down the road. – Michael Fourre Sep 17 '19 at 21:26
  • Hey @matt - thanks for the answer. I edited my question with pictures to better describe what I'm trying to do – spitchay Sep 17 '19 at 21:46