Change frame programmatically with auto layout
Solution 1
Let your UITableView
constraints to bottom layout is set to 0, make an IBOutlet
. Now let your GADBannerView
height is 40 so change your outlet.constant = 40;
For more about how to make IBOutlet
and change its value have a look into this or this hope this will help.
Edit: For those who seeking for example, follow these simple steps (Because this is accepted answer, I think it is worth to have an example. Credit to @manujmv for this example)
Create a height constraint for your view in your interface.
-
Then add an IBOutlet object in your class for this constraint. For example:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
Connect this object in your connection panel.
-
Then change the value of this constraint whenever you needed
self.heightConstraint.constant = 40;
Solution 2
Rather than trying to change the frame of the view, add a height constraint using auto layout and reduce the value of this constraint. Do the below steps:
- create a height constarint for your view in your interface.
-
Then add an IBOutlet object in your class for this constraint. for example,
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraint;
Connect this object in your connection panel.
-
Then change the value of this constraint whenever you needed
self.heightConstraint.constant = 40;
Solution 3
One more thing, you have to call [self.view layoutIfNeeded];
method once you changne the constraints.
Enjoy :)
Solution 4
You can find out constraint like that
extension UIView {
var heightConstaint: NSLayoutConstraint? {
get {
for constraint: NSLayoutConstraint in constraints {
if constraint.firstAttribute == .height {
if constraint.relation == .equal {
return constraint
}
}
}
return nil
}
set{
setNeedsLayout()
}
}
}
Related videos on Youtube
Comments
-
Fry almost 2 years
I have a
UITableView
with Auto Layout and I need to reduce the height when theGADBannerView
appears at the bottom of the screen.Unfortunately with Auto Layout it is impossible to modify the frame. The solution is to remove Auto Layout and set the frame manually. This is very dangerous because all my apps works fine on 3.5' and 4.0' displays and removing Auto Layout adds a new testing phase and more effort.
Is there a way to change the frame even if Auto Layout is enabled?
-
Bharat about 10 yearscreate an outlet for your constraint and change its value whenever your
GADBannerView
appear. -
Fry about 10 yearscan you explain better with an example please ?
-
AshvinGudaliya over 5 years
-
-
John Rogers about 9 yearsA bit late to the party, but adding
self.translatesAutoresizingMaskIntoConstraints = YES;
to your view would have probably removed the need for modifying constraint constants. -
Benjamin over 8 years@JohnRogers comment is actually the correct answer !