在tabview控制器中禁用自动旋转视图

时间:2014-07-11 17:35:40

标签: xcode ios7

您好我如何禁用tabview控制器中的视图的自动旋转?

我已经尝试在导航类中禁用,但那是不可能的。

这是我在AppDelegate.m中的didFinishLaunching。

我希望每个人都有一个想法?

谢谢!

- (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;
}

2 个答案:

答案 0 :(得分:1)

禁用整个UIViewController自动旋转

UIViewController中嵌入的UITabBarController依赖于最后一个控制器来处理-supportedInterfaceOrientations消息。它不是 ;基本上与这篇文章中的问题相同:iOS 6 rotations: supportedInterfaceOrientations doesn´t work?

您必须继承UITabBarController,并添加此代码以查询每个标签:

// 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];
}

当然,您仍然必须遵守常规自动旋转规则并在plist中设置标志。

对于您希望阻止方向更改的每个UIViewController子类,请按以下方式回复supportedInterfaceOrientations

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

有关详细信息,请参阅此帖子:Handling autorotation for one view controller in iOS7

答案 1 :(得分:0)

旋转除一个或多个子视图

之外的UIViewController

使用此方法:

-(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"];
}

从这里调用它:

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