ios 如何强制 UIView 的 layoutSubviews。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7570460/
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-30 21:39:38  来源:igfitidea点击:

How to force layoutSubviews of UIView.

iosiphoneuiviewuiviewcontrollerlayoutsubviews

提问by myell0w

I have a custom UIView which has a dedicated, manually set frame for portrait and landscape orientation because autoresizingMasks just don't work in my case.

我有一个自定义 UIView,它有一个专用的、手动设置的纵向和横向框架,因为 autoresizingMasks 在我的情况下不起作用。

I set this frame in:

我将此框架设置为:

 - (void)viewWillAppear:(BOOL)animated

And it works as expected.

它按预期工作。

My problem is now, that my UIView furthermore has subviews that also have a dedicated position for portrait/landscape.

我现在的问题是,我的 UIView 还具有子视图,这些子视图也有专门的纵向/横向位置。

I position these subviews in:

我将这些子视图定位在:

- (void)layoutSubviews

If I now leave my viewController in e.g.portrait orientation, rotate to landscape on another viewController and then come back to this viewController I see that my view got resized correctly, but the subviews of my view, which get positioned in layoutSubviews are not repositioned yet but are resized in an animated fashion after the view already appeared.

如果我现在离开我的viewController在例如纵向,旋转为横向另一个的viewController,然后回来这个的viewController我看到我的观点得到了正确的调整,但我的观点的子视图,其中获得定位在layoutSubviews尚未重新定位,但在视图出现后以动画方式调整大小。

I do nothing in viewDidAppear and I already tried calling

我在 viewDidAppear 中什么都不做,我已经尝试调用

 - (void)layoutIfNeeded

As well as:

也:

 - (void)setNeedsLayout

In viewWillAppear, but it doesn't seem to change anything.

在 viewWillAppear 中,但它似乎没有改变任何东西。

Any ideas what I'm doing wrong?

任何想法我做错了什么?

Thanks.

谢谢。

回答by blacky85

Had a similar problem today. I solved it by first calling

今天遇到了类似的问题。我先打电话解决了

  • -setNeedsLayout: Set the flag that the view needs layout, and afterwards calling
  • -layoutIfNeeded: Check the flag immediately and - as it was set manually before - as result layoutSubviews is called.
  • -setNeedsLayout: 设置视图需要布局的标志,然后调用
  • -layoutIfNeeded:立即检查标志 - 因为它之前是手动设置的 - 结果 layoutSubviews 被调用。

Don't know if this is the best way, but it works ;)

不知道这是否是最好的方法,但它有效;)

回答by Chris Miles

I would suggest setting the initial frame in viewDidLoad then changing the view frame for orientation changes in

我建议在 viewDidLoad 中设置初始框架,然后更改视图框架以进行方向更改

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

You don't need any explicit animations in layoutSubviews (not sure if you had any, but it sounded like it) as any frame changes will be synced to the rotation animation.

您不需要 layoutSubviews 中的任何显式动画(不确定是否有,但听起来像),因为任何帧更改都将同步到旋转动画。

When off screen the orientation should still be rotated, so your views should be positioned correctly when transitioned back on screen again.

在屏幕外时,方向仍应旋转,因此当再次转换回屏幕时,您的视图应正确定位。

I also recommend watching WWDC 2010 Session 123 "Building Animation Driven UI" which covers this stuff in much more detail.

我还建议您观看 WWDC 2010 Session 123“Building Animation Driven UI”,其中更详细地介绍了这些内容。

回答by Marco Aurelio Americo

At swift 3 @iOS 10.1

在 swift 3 @iOS 10.1

override func viewWillAppear(animated: Bool){

    super.viewWillAppear(animated)
    view.setNeedsLayout()
}

runs perfect

运行完美

回答by Gautam Jain

For me what worked is [self.view setNeedsDisplay]. This calls redraw on the whole view. You can also redraw a selected view by calling setNeedsDisplayon the particular view, which I did in my case and it worked perfectly.

对我来说有效的是[self.view setNeedsDisplay]. 这会在整个视图上调用重绘。您还可以通过调用setNeedsDisplay特定视图来重绘选定的视图,在我的案例中我就是这样做的,并且效果很好。