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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:08:17  来源:igfitidea点击:

How to get real size UIView with autolayout

iosautolayoutios-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 UIViewwith 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 UIViewControllerand not inside a UIViewsubclass, the answers don't prove really useful. The correct way to get the correct size of a UIViewsubclass is in layoutSubviews.

由于每个人都假设访问是通过 aUIViewController而不是在UIView子类中完成的,因此答案并没有证明真正有用。获得UIView子类正确大小的正确方法是在layoutSubviews.

回答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.

返回值
满足它所持有的约束的视图的大小。

讨论
考虑视图持有的所有约束及其子视图的约束,确定视图的最佳大小。

https://developer.apple.com/documentation/uikit/uiview/1622624-systemlayoutsizefittingsize?language=objc

https://developer.apple.com/documentation/uikit/uiview/1622624-systemlayoutsizefittingsize?language=objc

回答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 viewDidLayoutSubviewsbecause it happens before the viewDidAppearso 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 和作者。