UISplitViewController stops auto-rotation after trying to use UINavigationController inside it

I am having problems with the UISplitViewController in an iPad app. I am trying to make a simple navigation tree using the UINavigationController inside a UISplitView. For this, I used the following base code:

NavController.h

@interface NavController : NSObject {
    /* 
     * This is connected properly to the UINavigationController in the 
     * UISplitViewController through Interface Builder.
     */

    UINavigationController *navigationController;

 }

 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

 @end

NavController.m

#import "NavController.h"

@implementation NavController

@synthesize navigationController;

- (void) awakeFromNib {
    UIViewController *testController = [[UIViewController alloc] init];
    UITableView *tableView = [[UITableView alloc] init];

    [testController setView: tableView];

    [navigationController pushViewController: testViewController
                                    animated: YES];

}

@end

This code successfully displays the view on the navigation controller, and I can go back using the "Back" button, however my problem arises because after that my UISplitViewController no longer automatically rotates or rotates altogether from the portrait position. When I delete this code (and the view is not clicked), it works as expected.

What am I doing wrong and am I going to do it right?

!

+3
1

. , , . 1) 2) .

( .m ):

@interface UITabBarController (MyApp)
@end

@interface UINavigationController (MyApp)
@end

@implementation UITabBarController (MyApp) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
@end

@implementation UINavigationController (MyApp) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
@end

. , , , . - :

- (void)deviceOrientationDidChangeWithAnimation:(BOOL)animated {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (orientation == oldOrientation) {
        return;
    }

    if (animated) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        [UIView setAnimationDidStopSelector:@selector(orientationChanged)];
    }

    [self sizeToFitOrientation:YES];

    if (animated) {
        [UIView commitAnimations];
    }

    oldOrientation = orientation;
}

- (CGAffineTransform)transformForOrientation {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5); // rotated CCW
    } else if (orientation == UIInterfaceOrientationLandscapeRight) { // CW
        return CGAffineTransformMakeRotation(M_PI/2);
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { // CCW
        return CGAffineTransformMakeRotation(-M_PI);
    } else { // CW
        return CGAffineTransformIdentity;
    }
}

- (void)sizeToFitOrientation:(BOOL)transform {
    if (transform) {
        self.view.transform = CGAffineTransformIdentity;
    }

    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));

    CGFloat width = frame.size.width - 0 * 2;
    CGFloat height = frame.size.height - 0 * 2;

    UIInterfaceOrientation _orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(_orientation)) {
        self.view.frame = CGRectMake(0, 0, height, width);
    } else {
        self.view.frame = CGRectMake(0, 0, width, height);
    }
    self.view.center = center;

    if (transform) {
        self.view.transform = [self transformForOrientation];
    }
}

, ! - , ( , ), .:)

+1

Source: https://habr.com/ru/post/1782401/


All Articles