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)
Step 2 :- Set constrain to Label 1
1) Leading 2) Top 3) Trailing (From it's superview)
Step 3 :- Set constrain to Label 2
1) Leading 2) Top 3) Trailing (From it's superview)
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.
Step 5 :- (Optional) Set constrain to UIButton
1) Leading 2) Bottom 3) Trailing 4) Fixed Height (From mainview)
Output :-
Note :- Make sure you have set Number of lines =0 in Label property.
Edit1 :- If you want to increase size with BottomConstrain (>=) please check below 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
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:
- I need to create an IBOutlet of my heightConstraint
- Change the value of that constraint accordingly
Thank you all!
Related Topics
How to Get Data to Return from Nsurlsessiondatatask in Swift
Auto Height Does Not Work in UIview in Swift 3
Realitykit Entity Synchronization Is Always Nil
Tapping Is Required Twice to Uncheck Table Cell
How to Use Mtlblitcommandencoder for Copying Interlaced Video Fields into a Mtlbuffer
Xcode Swift: Could Not Insert New Outlet Connection
Skipnext Skipprevious Google Cast Greyed Out
Syntax to Create Dictionary in Swift
Didbegincontact Not Being Called Swift
Why Does Editing a Textfield Throw a Nsinvalidargumentexception
Must Call a Designated Initializer of The Superclass 'Day' Error
Protocol with Associatedtype Protocol for Generic Functions
Uirefreshcontrol Glitching in Combination with Custom Tableviewcell
Cannot Use Mutating Member on Immutable Value of Type 'string'
Audiokit Seems to Receive Only The First Three Numbers of Sysex Midi Messages
Downloading PDF Using Nsurlsession (Http Post) with Parameters