简体   繁体   English

如何强制 UIView 的 layoutSubviews?

[英]How to force layoutSubviews of UIView?

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 eg 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?任何想法我做错了什么?

Had a similar problem today.今天遇到了类似的问题。 I solved it by first calling我先打电话解决了

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

Don't know if this is the best way, but it works ;)不知道这是否是最好的方法,但它有效;)

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”,其中更详细地介绍了这些内容。

At swift 3 @iOS 10.1在 swift 3 @iOS 10.1

override func viewWillAppear(animated: Bool){

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

runs perfect运行完美

For me what worked is [self.view setNeedsDisplay] .对我来说,有效的是[self.view setNeedsDisplay] This calls redraw on the whole view.这会在整个视图上调用重绘。 You can also redraw a selected view by calling setNeedsDisplay on the particular view, which I did in my case and it worked perfectly.您还可以通过在特定视图上调用setNeedsDisplay来重绘选定的视图,我在我的例子中就是这样做的,并且效果很好。

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

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