A view jumps on top after a clicking on a button

55 Views Asked by At

My setup is a UITabBarController with two ViewControllers connected. setup

On one of the ViewControllers I've added a ContainerView constrained to the tab bar with a Blue Subview and a button on a subview. Constraints are here constraints

Then I add the containerView to keyWindow to show it on top of other ViewControllers like this:

        containerView!.translatesAutoresizingMaskIntoConstraints = false
        UIApplication.shared.keyWindow?.addSubview(containerView!)

After adding the containerView to the keyWindow I press the button on a Blue view and the view jumps on top of the screen. What am I missing here?

What button does:

        @IBAction func changeColor(_ sender: Any) {
       blueView.backgroundColor = UIColor.darkGray
        }

NOTES: If I set a button picture to project asset and not system asset the view doesn't jump on top. assets Also, if I don't add the view to the keyWindow the view doesn't jump on top with whatever asset.

1

There are 1 best solutions below

0
Thang Phi On

In here like I see you use autolayout for constraint. So you need to remove translatesAutoresizingMaskIntoConstraints to get the contsraints working.

So you need to remove this line

// remove this
// containerView!.translatesAutoresizingMaskIntoConstraints = false