AeyKung Taemin AeyKung Taemin - 1 year ago 72
Swift Question

swift: Remove Ads Banner and can not make App becomes Full Screen

My apps has a Root View Controller that contains Container View and Ads Banner. Container View's bottom is pinned to Ads Banner's top using Auto Layout.

My requirement is when Ads Banner is removed, the container view must resize to full screen.

I can't do it. I removed Ads Banner by either set it to nil or remove it from superview. But the Container View still remains the same size and same position, leaving a blank space at the bottom where Ads Banner was.

I also tried set new constraint to Container View with this code

containerViewBottomConstraint = NSLayoutConstraint(item: containerView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view.superview, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)

But then the App crashes with this error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs.


Answer Source

Lets say that the Ad Banner height is 50, so just get the bottom constraint of the container view as an outlet to your class.

Then adjust the constraint by doing like:

containerViewBottomConstraint.constant = -50

You might need to call self.view.layoutIfNeeded() in order to refresh the view with new constraint.

Good luck !