Onichan
Onichan

Reputation: 4516

View Position Resets with bringSubviewToFront [AutoLayout]

Using Autolayout, everytime I call bringSubviewToFront, the UIView resets.

I tried setting an original position with txtViewPosition:

        txtViewPosition = CGRect(x: txtView.frame.origin.x, y: txtView.frame.origin.y, width: txtView.frame.size.width, height: txtView.frame.size.height)
        containerView.bringSubviewToFront(txtView)
        txtView.frame = txtViewPosition!
        

It looks like the view is the correct position, but it is still showing up in its original spot (set in AutoLayout).

Update (UIView still snaps back)

override func viewDidLayoutSubviews() {
    if txtViewPosition != nil {
        println("txt position is \(txtViewPosition)")
        txtView.frame = txtViewPosition!
    }
}

Upvotes: 3

Views: 612

Answers (2)

Artin
Artin

Reputation: 241

Both view.bringSubviewToFront(txtView) and view.sendSubviewToBack(txtView) reset view position if you animate the view position instead of the constraints. You can use txtView.layer.zPosition instead. The default value is 0. Giving a higher number will bring the view to front.

Upvotes: 0

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

If you need to have finer grain control over the layout then you need to makes the changes in -layoutSubviews or -viewDidLayoutSubviews.

Upvotes: 2

Related Questions