Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed height constraint on UIView

I have a UIView inside of my layout in order to do some clipping and grouping, however the autolayout resizes it when shrunk. I want to give it a fixed height but the only option is to set the top and bottom space.

Is there a way to set an explicit height constraint?

like image 766
Steven Avatar asked Jan 09 '13 23:01

Steven


People also ask

How do you add constraints in Uikit?

Control-Dragging Constraints To create a constraint between two views, Control-click one of the views and drag to the other. When you release the mouse, Interface Builder displays a HUD menu with a list of possible constraints.

How do I update the layout in Swift?

If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method.


1 Answers

I just found this answer (Auto-Layout Constraint: How to make a view maintains its width/height ratio when resized?). 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];
}
like image 124
ebandersen Avatar answered Sep 20 '22 04:09

ebandersen