Enzoses
Enzoses

Reputation: 115

How to translate viewWillLayoutSubviews to viewWillTransitionToSize

I'm using viewWillLayoutSubviews to detect orientation changes but as in iOS 8 and Xcode 6 is deprecated I need to use new viewWillTransitionToSize. My problem is that I'm not able to use it.

My viewWillLayoutSubviews is:

- (void)viewWillLayoutSubviews{
    NSInteger maxQuantityForWidth = MAX(3, [self.barButtons count]);
    CGFloat buttonWidth = (CGRectGetWidth(self.tabBar.bounds) - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
    for (NSInteger i = 0; i < [self.barButtons count]; i++) {
        [self.barButtons[i] setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
    }
    if ([self isShowingTabBar]) {
        self.functionalityContainer.frame = CGRectMake(0.0, CGRectGetHeight(self.tabBar.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.tabBar.bounds));
    } else {
        self.functionalityContainer.frame = self.view.bounds;
    }

    if (IS_IPAD){
        [self updateToolbar];
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        if (LANDSCAPE) {
            frame.size.width = 700;
        } else {
            frame.size.width = 768;
        }
        [self.slidingViewController.topViewController.view setFrame:frame];
    }
  }

and it's part of the Navigation section using ECSLidingViewController.

I tried the following:

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self updateToolbar];

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        if (IS_IPAD){
            CGRect frame = self.slidingViewController.topViewController.view.frame;

            if (LANDSCAPE) {
                NSInteger maxQuantityForWidth = MAX(3, [self.barButtons count]);
                CGFloat buttonWidth = (758 - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
                for (NSInteger i = 0; i < [self.barButtons count]; i++) {
                    [self.barButtons[i] setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
                }
                frame.size.width = 768;
            } else {
                NSInteger maxQuantityForWidth = MAX(3, [self.barButtons count]);
                CGFloat buttonWidth = (758 - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
                for (NSInteger i = 0; i < [self.barButtons count]; i++) {
                    [self.barButtons[i] setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
                }
                frame.size.width = 768;
            }
            [self.slidingViewController.topViewController.view setFrame:frame];
        }

        if([self isShowingTabBar]){
            [self showTabBar];
        }else{
            [self hideTabBar];
        }
        if (IS_IPAD && LANDSCAPE) {
            self.slidingViewController.resetStrategy = ECNone;
            [self.navigationItem setLeftBarButtonItems:@[] animated:YES];
            if (!self.slidingViewController.underLeftShowing) {
                [self.slidingViewController anchorTopViewTo:ECRight];
            }
        } else {
            self.slidingViewController.resetStrategy = ECTapping | ECPanning;
            [self.navigationItem setLeftBarButtonItems:@[self.displayMenuBarButtonItem] animated:YES];
            [self.slidingViewController resetTopView];
        }
        [self.slidingViewController.topViewController.view setNeedsDisplay];
        [self.functionalityNavigationController.view setNeedsDisplay];

        [self.view setNeedsDisplay];
    }];

    NSString *orientation = [[NSString alloc]initWithFormat:@"Larghezza: %f Altezza: %f", self.view.frame.size.width, self.view.frame.size.height];
    [self.view makeToast:orientation duration:3.0 position:@"bottom"];
    [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
}

but it doesn't work, it seems doesn't "understand" when it's landscape or portrait. Any idea on how to fix it?

*** EDIT:

here the definitions I use

#define LANDSCAPE UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
#define LANDSCAPE_RIGHT [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft
#define LANDSCAPE_LEFT [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight
#define PORTRAIT UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
#define PORTRAIT_REVERSE [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown

Upvotes: 2

Views: 847

Answers (2)

malhal
malhal

Reputation: 30719

You should be using the trait collection method and then check the size class.

/* 
 This method is called when the view controller's trait collection is changed by its parent.

 If you override this method, you should either call super to propagate the change to children or manually forward the change to children.
 */
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

Upvotes: 0

user2831473
user2831473

Reputation: 168

The question seems a bit "old", anyway, at least with the introduction of iOS9 (especially multitasking), it is not recommended to draw conclusions regarding the current screen format based on device-orientation - instead, one should evaluate the current screen size. The following indicates whether the screen format is portrait or rather landscape-ish:

- (void) viewWillTransitionToSize : (CGSize) screenSize withTransitionCoordinator : (id<UIViewControllerTransitionCoordinator> _Nonnull) coordinator
{
    BOOL isLandscapeOrientation = (screenSize.width > screenSize.height);
    ...
    ...
}

Upvotes: 1

Related Questions