ios 如何使用自动布局获得真实大小的 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27653247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get real size UIView with autolayout
提问by Chicken
I'm a new developer iOS.
我是一个新的开发者 iOS。
I use auto-layout.
我使用自动布局。
I have a problem. I can not get real size of UIView after constraints.
我有个问题。约束后我无法获得 UIView 的实际大小。
Example, when I want get size of UIView
with constraint
示例,当我想获得UIView
带约束的大小时
self.container.frame.size.height
It alway return 550 for all device size. But I can see it have difference size when I run it on difference device.
对于所有设备大小,它始终返回 550。但是当我在不同的设备上运行它时,我可以看到它的大小不同。
Thanks
谢谢
采纳答案by Oxcug
The simplest solution is to just run that code in -viewDidAppear:
(after autolayout has been run).
最简单的解决方案是运行该代码-viewDidAppear:
(在运行自动布局之后)。
回答by Allen
Before your "getting real size code", insert the following code:
在“获取实际尺寸代码”之前,插入以下代码:
[view setNeedsLayout];
[view layoutIfNeeded];
回答by Leandros
Since everybody assumes the access is done via a UIViewController
and not inside a UIView
subclass, the answers don't prove really useful. The correct way to get the correct size of a UIView
subclass is in layoutSubviews
.
由于每个人都假设访问是通过 aUIViewController
而不是在UIView
子类中完成的,因此答案并没有证明真正有用。获得UIView
子类正确大小的正确方法是在layoutSubviews
.
回答by Abizern
You could just use the UIView
method intrinsicContentSize
你可以只使用该UIView
方法intrinsicContentSize
which you can read about https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/intrinsicContentSize
回答by Dustt
Seems like - (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize;
is what you are looking for:
似乎- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize;
是你在找什么:
Return Value
The size of the view that satisfies the constraints it holds.Discussion
Determines the best size of the view considering all constraints it holds and those of its subviews.
返回值
满足它所持有的约束的视图的大小。讨论
考虑视图持有的所有约束及其子视图的约束,确定视图的最佳大小。
回答by SamSol
This is awesome.
这太棒了。
If you want to get frame without using layoutsubviews method during autolayout usage, Use this one:
如果您想在自动布局使用期间不使用 layoutsubviews 方法获取框架,请使用以下方法:
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"View frame: %@", NSStringFromCGRect(view.frame));
})
回答by Zaphod
The best moment is during the viewDidLayoutSubviews
because it happens before the viewDidAppear
so you still are able to do some modifications before the view is displayed.
最好的时刻是在viewDidLayoutSubviews
之前,因为它发生在 之前,viewDidAppear
因此您仍然可以在显示视图之前进行一些修改。
回答by Chiakie
[view layoutIfNeed] save me.
[查看 layoutIfNeed] 救救我。
Use this method to force the layout of subviews before drawing. Using the view that receives the message as the root view, this method lays out the view subtree starting at the root.
使用此方法在绘制之前强制子视图的布局。该方法使用接收消息的视图作为根视图,从根开始布置视图子树。
After invoke this method, I can get actual size of custom view in xib. Thanks a lot to @Allen and the author.
调用此方法后,我可以在 xib 中获取自定义视图的实际大小。非常感谢@Allen 和作者。