Fixed height limit on UIView

I have a UIView inside my layout to do some clipping and grouping, however auto-detection resizes it when compressed. I want to give it a fixed height, but the only option is to set the upper and lower space.

Is there a way to set an explicit height limit?

+4
source share
7 answers

I just found this answer ( Auto Layout Restriction: How do I make the view maintain the ratio of width to height when resizing? ). This is what my implementation looks like:

- (void)awakeFromNib { [super awakeFromNib]; NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:self.frame.size.height]; [self addConstraint:con1]; } 
+4
source
  • Select your view in the Inspector to the left of Interface Builder.
  • Ctrl drag the line from the view to itself.

=> A popover will appear where you can set explicit height and width limits.

+3
source

Yes and no. In AutoLayout, you cannot set the size limit for a UIView, however you can prevent the UIView from “shrinking” according to its internal size .

This will effectively limit the view, while avoiding the lack of a parent view, so that the child view has a certain size (and thus removes the “Auto” part of AutoLayout).

To set these priorities, you use: setContentCompressionResistancePriority:forAxis:

From Apple UIView Documentation :

Custom views should set default values ​​for both orientations when creating based on their contents, usually for NSLayoutPriorityDefaultLow or NSLayoutPriorityDefaultHigh. When creating user interfaces, the layout designer can change these priorities for certain views when the overall layout design requires different compromises than the natural priorities of the views used in the interface.

Subclasses should not override this method.

So, now we know how to assign priority in order to avoid our view becoming smaller than its internal size, but how to set our own size?

Well, if you use the standard user interface element, you are already configured! But if your UIView is normal, you need to override - (CGSize)intrinsicContentSize to return the correct size. Here you can measure any subviews that need to calculate the correct sizes, or if you use art / constant size elements, you can return a solid value.

Again, from the Apple UIView Documentation :

Custom views typically contain content displayed on a screen that the layout system is not aware of. Overriding this method allows the custom view to communicate with the layout, what size it would like to use on its content. This internal size must be independent of the content frame, because there is no way to dynamically link the changed width to the layout system based on the changed height, for example.

Apple strongly recommends that you don’t check anything outside of your UIView (for example, getting the size of your superview and adjusting it), as this is not what AutoLayout is for (and can lead to a bad headache along the way).

+2
source

Yes, you can give it a fixed height.

You need to specify at least two constraints in each dimension, so you cannot remove the upper or lower space until you add a fixed height constraint. In IB, select your presentation, then a button with three icons will appear in the lower right corner of the view editor, click middle and select "Height". This adds a height restriction. Now you can go and remove the lower or upper space, and you can edit the height limit if you need by clicking it in the dimension inspector.

+1
source

You do not need to specify the upper space and lower space. If you have a lower limit, for example, 8 pixels from the bottom of the supervisor, it will always try to resize the child view to remain in the super view (depending on whether you set priorities).

You can assign a height constraint to a priority above a lower space constraint, so when they conflict, it will choose a forced height constraint and ignore the lower constraint.

For restrictions, you do not need 4 restrictions (top, bottom, left and right); you only need 2: vertical and horizontal. The rest is calculated by automatic check out.

Just remove the bottom constraint and the child view should be cropped.

0
source

Fast eckyzero code.

 let heightConstraint = NSLayoutConstraint(item: yourView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 80)//your desired height yourView.addConstraint(heightConstraint) 
0
source

try it

 view.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
-4
source

All Articles