Disable autorotate for view in tabview controller

强颜欢笑 提交于 2020-01-07 05:28:09

问题


Hi how i can disable the autorotation for a view in a tabview controller??

i have testet to disable in a navigationclass, but thats not possible.

Thats my didFinishLaunching in the AppDelegate.m.

I hope everyone have an idea??

Thanks!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES"
                                                        forKey:@"myKeyName"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];

[self setupFetchedResultsController];

if (![[self.fetchedResultsController fetchedObjects] count] > 0 ) {
    NSLog(@"!!!!! ~~> There's nothing in the database so defaults will be inserted");
    [self importCoreDataDefaultRoles];
    [self importCoreDataDefaultMaterials];
    [self importCoreDataDefaultPersons];
}
else {
    NSLog(@"There's stuff in the database so skipping the import of default data");
}

// TAB BAR
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

    NSLog(@"I'm an iPad");

    // *** Set up the Persons Split Views (2-Way Delegation & Pass Managed Object Context) *** //

    // Set up SPLIT VIEW for Persons
    UISplitViewController *splitViewController = [[tabBarController viewControllers] objectAtIndex:0];

    // Set up Split View MASTER view for Persons
    UINavigationController *personsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0];
    splitViewController.delegate = (id)personsMasterTVCnav.topViewController;
    PersonsTVC *personsTVC = [[personsMasterTVCnav viewControllers] objectAtIndex:0];
    personsTVC.managedObjectContext = self.managedObjectContext;

    // Set up Split View DETAIL view for Persons
    UINavigationController *personsDetailTVCnav = [splitViewController.viewControllers objectAtIndex:1];
    PersonDetailTVC *personDetailTVC = [personsDetailTVCnav.viewControllers objectAtIndex:0];

    // Set up MASTER and DETAIL delegation so we can send messages between views
    personsTVC.delegate = personDetailTVC;
    personDetailTVC.delegate = personsTVC;

    // *** Set up the Roles Views *** (Pass Managed Object Context)//
    UINavigationController *rolesTVCnav = [[tabBarController viewControllers] objectAtIndex:1];
    RolesTVC *rolesTVC = [[rolesTVCnav viewControllers] objectAtIndex:0];
    rolesTVC.managedObjectContext = self.managedObjectContext;

    // *** Set up the Materials Views *** (Pass Managed Object Context)//
    UINavigationController *materialsTVCnav = [[tabBarController viewControllers] objectAtIndex:2];
    MaterialsTVC *materialsTVC = [[materialsTVCnav viewControllers] objectAtIndex:0];
    materialsTVC.managedObjectContext = self.managedObjectContext;

    // Set delegate for splitViewController
    splitViewController.delegate = personDetailTVC;


}
else
{
    NSLog(@"I'm an iPhone or iPod Touch");

    // The Two Navigation Controllers attached to the Tab Bar (At Tab Bar Indexes 0 and 1)
    UINavigationController *personsTVCnav = [[tabBarController viewControllers] objectAtIndex:0];
    UINavigationController *rolesTVCnav = [[tabBarController viewControllers] objectAtIndex:1];
    UINavigationController *materialsTVCnav = [[tabBarController viewControllers] objectAtIndex:2];
    // The Persons Table View Controller (First Nav Controller Index 0)
    PersonsTVC *personsTVC = [[personsTVCnav viewControllers] objectAtIndex:0];
    personsTVC.managedObjectContext = self.managedObjectContext;


    // The Roles Table View Controller (Second Nav Controller Index 0)
    RolesTVC *rolesTVC = [[rolesTVCnav viewControllers] objectAtIndex:0];
    rolesTVC.managedObjectContext = self.managedObjectContext;

    // The Materials Table View Controller (Third Nav Controller Index 0)
    MaterialsTVC *materialsTVC = [[materialsTVCnav viewControllers] objectAtIndex:0];
    materialsTVC.managedObjectContext = self.managedObjectContext;
}

return YES;
}

回答1:


Rotating a UIViewController except one or more subviews

Use this method:

-(void)counterRotateView:(UIView *)view
    toInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    duration:(NSTimeInterval)duration
{
    NSParameterAssert(view);

    CALayer* layer = view.layer;
    CABasicAnimation* animation;
    animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

    CGFloat tau = 0;
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            tau = 0.5; break;
        case UIInterfaceOrientationLandscapeRight:
            tau = -0.5; break;
        case UIInterfaceOrientationPortraitUpsideDown:
            tau = 1; break;
        case UIInterfaceOrientationPortrait:
        default:
            break;
    }
    animation.toValue = [NSNumber numberWithFloat:tau * M_PI];

    animation.duration = duration;
    animation.cumulative = YES;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;

    [layer addAnimation:animation forKey:@"transform.rotation.z"];
}

And invoke it from here:

-(void) willRotateToInterfaceOrientation:
    (UIInterfaceOrientation)toInterfaceOrientation
    duration:(NSTimeInterval)duration
{
    [self counterRotateView:someView
        toInterfaceOrientation:toInterfaceOrientation
        duration:duration];
}



回答2:


Disabling entire UIViewController auto-rotation

A UIViewController embedded in a UITabBarController is relying upon that last controller to handle the -supportedInterfaceOrientations messages. It is not ; basically the same problem as in this post: iOS 6 rotations: supportedInterfaceOrientations doesn´t work?

You must subclass your UITabBarController, and add this code to query each tab:

// In UITabBarController subclass
- (BOOL)shouldAutorotate;
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController * top;
    UIViewController * tab = self.selectedViewController;
    if([tab isKindOfClass:
        ([UINavigationController class])]) {
        top = [((UINavigationController *)tab)
                 topViewController];
    }

    if ([top respondsToSelector:@selector(supportedInterfaceOrientations)])
        return [top supportedInterfaceOrientations];
    else
        return [super supportedInterfaceOrientations];
}

Of course, you must still respect the general auto-rotation rules and set the flags in the plist.

For each UIViewController subclass you want to prevent orientation changes, respond to supportedInterfaceOrientations as so:

// In UIViewController subclass
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
    // Or whatever orientation you support
}

See this post for further details: Handling autorotation for one view controller in iOS7



来源:https://stackoverflow.com/questions/24703427/disable-autorotate-for-view-in-tabview-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!