Search code examples
iosrotationautoresizingmaskviewwilltransitiontosize

Autoresizingmask, viewWillTransitionToSize and rotation


I have parentViewController and it contains Container with customViewController with autoresizing masks (flexible width and height). Container's width is ~80% of parentViewController view. When I rotate device viewWillTransitionToSize:withTransitionCoordinator: is called but with "wrong" size. I get parentsViewController view size.

Here is part of customViewController

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    NSLog(@"viewWillTransitionToSize %@", NSStringFromCGSize(size));
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [self resizeViewToWidth:size.width];
    [self performSelector:@selector(didRotate) withObject:nil afterDelay:1.0];
}

-(void)didRotate
{
    NSLog(@"didRotate %@", NSStringFromCGSize(self.view.frame.size));
}

//it creates this log
//viewWillTransitionToSize {667, 331}
//didRotate {617, 311}   //this is correct size after applying autoresizing masks

Why is it called only once when I see at least two different sizes? I don't want to calculate view size if autoresizing masks can do it for me.

How do I let apply autoresizing masks first and then get size after applying?

What is correct way of using viewWillTransitionToSize while using autoresizing masks?


Solution

  • I solved it following way. It is actually overriding autoresizing mask but I couldn't find better solution and this fits my requirements.

    I use method sizeForChildContentContainer:withParentContainerSize: of UIContentContainer. In method I check for child container and apply changes same as autoresizing would do by itself. magicNumber is abbreviation for custom calculation.

    -(CGSize)sizeForChildContentContainer:(id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize
    {
        if ([container isKindOfClass:[customViewController class]])
        {
            return CGSizeMake(parentSize.width - magicNumber, parentSize.height);
        }
        return [super sizeForChildContentContainer:container withParentContainerSize:parentSize];
    }