简体   繁体   English

自定义UIView,在Storyboard上具有动态高度

[英]Custom UIView with dynamic height on Storyboard

I want to implement custom UIView (from code) with dynamic height, based on content (similar to UILabel). 我想基于内容(类似于UILabel)实现具有动态高度的自定义UIView(来自代码)。 I can do this by implementing intrinsicContentSize - and it seems to work correctly, but... 我可以通过实现intrinsicContentSize来实现这一点 - 它似乎正常工作,但......

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. 将UILabel添加到情节提要板时,您不必指定高度,也没有错误。 How to achieve the same behavior for custom UIView (I want to use it in storyboard)? 如何为自定义UIView实现相同的行为(我想在故事板中使用它)?

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... 我刚刚发现Storyboard中存在“Intrinsic Size”属性 - 您只需将其设置为“Placeholder”并为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}; 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]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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