swiftuiimageviewuipageviewcontrolleruiviewanimation

imageView.layer.origin not working correctly in UIPageViewController's first view controller


I have a UIPageController that controls my view controllers (an array of custom type TipViewController).

On TipViewController, there is a UIImageView called pointerView. Its position is adjusted to a custom position (using layer.origin properties).

The very first view controller never updates the imageView's origin correctly because pointerView.frame.origin is always 0,0 even on viewDidAppear(). Returning to that first page randomly moves pointerView upwards, and printing the pointerView.frame.origin shows that it's completely out of sync to how it is appearing visually.

The rest of the pages appear correctly. I am baffled to why just the first page is not working? Is it something to do with the UIPageController?

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if origin == nil {
            print("Setting up origin for : \(data.name)")
            origin = CGPoint(x: pointerView.frame.origin.x, y: pointerView.frame.origin.y)
            origin?.x += data.pointerLocation.x
            origin?.y += data.pointerLocation.y
        }
        resetPointerView()
    }
    
    private func resetPointerView() {
        if let origin = origin {
            pointerView.frame.origin.x = origin.x
            pointerView.frame.origin.y = origin.y
        }
        pointerView.alpha = 1
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        print("     viewWillDisappear \(data.header)")
        super.viewWillDisappear(animated)
        resetPointerView()
        pointerView.layer.removeAllAnimations()
        pointerView.alpha = 0
    }

Solution

  •  private func resetPointerView() {
            view.layoutIfNeeded()
            if let origin = origin {
                pointerView.frame.origin.x = origin.x
                pointerView.frame.origin.y = origin.y
            }
            pointerView.alpha = 1
            
        }
    

    view.layoutIfNeeded() resolved this