简体   繁体   中英

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.

- (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. 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.

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.

I believe the iOS8+ viewWillTransitionToSize:withTransitionCoordinator: method fires even when offscreen, but I'm not positive. 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:

@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. 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

willRotateToInterfaceOrientation:duration:

And for iOS 8+

viewWillTransitionToSize:withTransitionCoordinator:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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