简体   繁体   中英

Custom UIView with dynamic height on Storyboard

I want to implement custom UIView (from code) with dynamic height, based on content (similar to UILabel). I can do this by implementing intrinsicContentSize - and it seems to work correctly, but...

When I am adding my custom view to view controller on storyboard, I've got error (missing height constraint) - even when during launch everything seems to resize correctly.

When you add UILabel to storyboard you don't have to specify height, there are no errors. How to achieve the same behavior for custom UIView (I want to use it in storyboard)?

If someone will need the solution:

I just found that there is "Intrinsic Size" property in Storyboard - you just need to set it to "Placeholder" and set some values just for Interface Builder...

在此输入图像描述

i come to know that you are creating by programmatically. try to use the following code and improve it in your requirement . Best of luck. NSDictionary* views = @{@"view" : self.view};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:0 views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:0 views:views]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM