Auto Height Does Not Work in UIview in Swift 3

How to programmatically increase the height of UIView with Swift

You need to create an outlet for the height constraint of the detail view, and then you can adjust the height programmatically like this myHeightConstraint.constant += extraHeight where extraHeight is a number indicating how much taller you want the detail view to be. You also may need to call self.view.layoutIfNeeded() afterwards.

To create an outlet from a constraint, control drag from the constraint in the storyboard editor (just like you would for an outlet of a UIView) into your code.

You are right - because you are using auto layout and constraints, the adjustments need to be made with constraints too. Setting the raw frames can lead to unexpected behavior. Let me know if you have any other questions or difficulties.

UIView width & height not adjusting to constraints

The important thing while using auto layout, is you should not be changing the frame manually anymore (using setCenter: in your case). You should layout everything using constraints.

You could add the UIImage in the storyboard on top of the mapView and center it there, using constraints.

If that is not possible, you can also add the constraints in code, using something like this:

[self.mapView addSubview:crosshairs];
[crosshairs setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mapView addConstraints:[NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:crosshairs attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
[self.mapView addConstraints:[NSLayoutConstraint constraintWithItem:self.mapView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:crosshairs attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];

Depending on where you are adding these constraints, you might have to call [self.mapView layoutIfNeeded];.

Using the constraints, the image should always be centered on top of the mapView.

About the wrong frame for the mapView, it depends where you are logging it. If your IBOutlet is in a UIViewController, you should do it in viewWillLayoutSubviews: after the [super viewWillLayoutSubviews]; call.

- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"Map View frame %@", NSStringFromCGRect(self.mapView.frame));
}

If you have it in a custom UIView class, you should check the frame in layoutSubviews:

- (void)layoutSubviews {
[super layoutSubviews];
NSLog(@"Map View frame %@", NSStringFromCGRect(self.mapView.frame));
}

Hope this fixes your problem.

Auto set height of container view based on subviews

You need to pin blueView's bottom to redView's bottom, just add this line to redView's updateConstraints:

NSLayoutConstraint(item: blueView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0).active = true

UIView with dynamic height multiple UILabel

I made a demo for you according to your requirement. Download it from below link,

Autoresize UIView and UILabel

Step by Step Guide :-

Step 1 :- Set constrain to UIView

1) Leading 2) Top 3) Trailing (From mainview)

Sample Image

Step 2 :- Set constrain to Label 1

1) Leading 2) Top 3) Trailing (From it's superview)

Sample Image

Step 3 :- Set constrain to Label 2

1) Leading 2) Top 3) Trailing (From it's superview)

Sample Image

Step 4 :- Most tricky give bottom to UILabel from UIView.

In Xcode 10 and above - Select your UIView and Label after that from the Align Constraint section select Bottom Edges constraint.

Sample Image

Sample Image

Step 5 :- (Optional) Set constrain to UIButton

1) Leading 2) Bottom 3) Trailing 4) Fixed Height (From mainview)

Sample Image

Output :-

Sample Image

Note :- Make sure you have set Number of lines =0 in Label property.

Sample Image

Edit1 :- If you want to increase size with BottomConstrain (>=) please check below Image.

Sample Image

I hope this info enough to understand Autoresize UIView according to UILabel's height and Autoresize UILabel According to text.

UIView dynamic height depending on Label Height

you can just do it with storyboard this pics

Sample Image

Sample Image

set the label height relation to greater than or equal

and set the view height relation to greater than or equal

it work like a magic

How to update the constant height constraint of a UIView programmatically?

Select the height constraint from the Interface builder and take an outlet of it. So, when you want to change the height of the view you can use the below code.

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

Method updateConstraints() is an instance method of UIView. It is helpful when you are setting the constraints programmatically. It updates constraints for the view. For more detail click here.

Why can't I change the view's frame size in Swift?

Ok, So I found a solution here on stack ..

Change frame programmatically with auto layout

The correct answer, that works is, that:

  1. I need to create an IBOutlet of my heightConstraint
  2. Change the value of that constraint accordingly

Thank you all!



Related Topics



Leave a reply



Submit