简体   繁体   English

sizeThat在视图上有约束(框架布局视图中的自动布局视图)

[英]sizeThatFits on view with constraints (auto layout view inside frame layout view)

这不可能做到

View A layouts it's subviews by calculating and setting their frames in layoutSubviews. 通过在layoutSubviews中计算和设置框架,查看A的子视图。

View B uses constraints to layout it's subviews. 视图B使用约束来布局它的子视图。

View A contains a number of View B instances. 视图A包含许多View B实例。

View A needs to know the size of its subviews when it layouts them, so it calls sizeThatFits: on them. 视图A在布局时需要知道子视图的大小,因此它会调用sizeThatFits: on。

View B's height depends on it's width. 视图B的高度取决于它的宽度。 In my example it has a multiline UILabel inside it. 在我的例子中,它内部有一个多行UILabel

How should B implement it's sizeThatFits: so it returns the correct size taking both the size parameter passed to sizeThatFits: and the constraints in account? B应该如何实现它的sizeThatFits:所以它返回正确的大小,同时将size参数传递给sizeThatFits:和帐户中的约束?

What I've tried/considered so far: 到目前为止我尝试/考虑的内容:

systemLayoutSizeFittingSize: - doesn't work because it doesn't take into account an exact size passed to it. systemLayoutSizeFittingSize: - 不起作用,因为它没有考虑传递给它的确切大小。 It can't be used to specify an exact width for eg a view that grows vertically as it shrinks horizontally. 它不能用于指定精确的宽度,例如,当水平收缩时垂直生长的视图。 It only gives either the most compressed size or the most expanded size. 它只提供最大压缩尺寸或最大尺寸。

intrinsicContentSize - this is used to pass information to Auto Layout as far as i understand from the docs. intrinsicContentSize - 据我所知,这可用于将信息传递自动布局。 What we want is to get information from Auto Layout. 我们想要的是 Auto Layout获取信息。

This is how i've reasoned so far. 到目前为止,这就是我的理由。 Please correct me if i've made a mistake. 如果我犯了错误,请纠正我。 And please help me, i'd be very grateful if someone could point me to how this is done! 请帮助我,如果有人能指出我是如何完成的,我将非常感激!

edit: Also, maybe there is some entirely different way to do this, without even using sizeThatFits: ? 编辑:此外,也许有一些完全不同的方式来做到这一点,甚至没有使用sizeThatFits: The only requirement i have is that i can continue using frame layout in the "outer" view (View A) and auto layout in the "inner view" (View B). 我唯一的要求是我可以在“外部”视图(视图A)中继续使用框架布局,在“内部视图”(视图B)中继续使用自动布局。

edit2: Added a picture to illustrate the exact case i must solve edit2:添加了一张图片来说明我必须解决的确切情况

edit3: Forgot to mention, i need to support iOS 7.0 edit3:忘了提,我需要支持iOS 7.0

From the picture it would appear that a UIStackView might be a better approach for View A? 从图中可以看出,UIStackView可能是View A的更好方法吗?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/ https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/

and then just constrain with a margin offset the bottom of the View A UIStackView to the bottom of the last View B 然后用一个边距约束,将View A UIStackView的底部偏移到最后一个View B的底部

I would use Autolayout for the lot. 我会使用Autolayout。 You can still specify exact sizes and positions, you just do it with constraints instead of with frames. 您仍然可以指定精确的大小和位置,只需使用约束而不是框架。

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

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