简体   繁体   English

子视图方向更改时如何旋转父视图

[英]How to rotate parent view when child view orientation changes

I'm having a problem adjusting a parent view's layout when orientation changes for it's child view. 当其子视图的方向更改时,调整父视图的布局时出现问题。 I have a collection view controller that, when one of the cells are tapped, pushes a child view on top. 我有一个集合视图控制器,当点击其中一个单元格时,会将子视图推到顶部。 If an orientation change occurs while the child view is visible and it is dismissed, the parent view's collection view cells haven't adjusted for the new width. 如果在子视图可见且关闭时方向发生变化,则父视图的集合视图单元格未针对新宽度进行调整。

I should note that this works fine if the parent view is visible. 我应该注意,如果父视图是可见的,则可以正常工作。

The only thing that has fixed this for me is in the viewDidAppear method of the parent view controller invalidates the collection view layout, but for me it's too late as the user sees the animation of the collection view cells snap into place. 唯一为我解决此问题的方法是在父视图控制器的viewDidAppear方法中使集合视图布局无效,但是对我来说,为时已晚,因为用户看到集合视图单元格的动画固定到位。

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self.collectionView.collectionViewLayout invalidateLayout];
    [self.collectionView reloadData];
}

I would have preferred to use viewWillAppear, but that doesn't seem to do anything. 我本来希望使用viewWillAppear,但是似乎没有任何作用。 It sounds like it can only adjust the cells when the parent view is visible. 听起来只能在可见父视图时调整单元格。

Is there a way around this? 有没有解决的办法?

Referring to this answer , iOS does not send orientation change events to offscreen view controllers, making them an unreliable way to determine whether the view has been resized. 参考此答案 ,iOS不会将方向更改事件发送到屏幕外的视图控制器,这使它们成为确定视图是否已调整大小的不可靠方法。

viewWillAppear: isn't working in your case because iOS doesn't resize the offscreen view controller's view until after it calls the method, so your invalidate and reload are being pulled off the wrong values. viewWillAppear:在您的情况下不起作用,因为iOS在调用该方法之后才调整屏幕外视图控制器视图的大小,因此您的无效和重新加载将被拉离错误的值。

I believe the iOS8+ viewWillTransitionToSize:withTransitionCoordinator: method fires even when offscreen, but I'm not positive. 我相信iOS8 + viewWillTransitionToSize:withTransitionCoordinator:方法即使在屏幕外也会触发,但我并不乐观。 In my experience, the size it provides does not reflect the actual size of the view. 以我的经验,它提供的大小并不反映视图的实际大小。 What I personally like to hook into is viewWillLayoutSubviews , usually guarded with a width check: 我个人比较喜欢的是viewWillLayoutSubviews ,通常通过宽度检查来保护它:

@property (nonatomic) CGFloat lastWidth;

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    if (self.lastWidth != CGRectGetWidth(self.view.bounds)) {
        self.lastWidth = CGRectGetWidth(self.view.bounds);

        // Update your collection view here.
    }
}

This way, whenever your view is going to resize (on display, inside an orientation change animation) you can update the size information. 这样,无论何时要调整视图大小(在显示中,在方向更改动画内),您都可以更新大小信息。

try overriding -(void)viewWillLayoutSubviews: method in your parent view controller. 尝试在父视图控制器中覆盖-(void)viewWillLayoutSubviews:方法。 In your case,it goes like this 在你的情况下,它像这样

-(void)viewWillLayoutSubviews {

[self.collectionView.collectionViewLayout invalidateLayout];[self.collectionView reloadData];
}

You could try invalidating your layout and reloading in the rotation handler methods. 您可以尝试使布局无效并在旋转处理程序方法中重新加载。

For pre-iOS 8 对于iOS 8之前的版本

willRotateToInterfaceOrientation:duration:

And for iOS 8+ 对于iOS 8+

viewWillTransitionToSize:withTransitionCoordinator:

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

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