tuvok
tuvok

Reputation: 699

Add UIView to window in landscape

My app starts with a UISplitViewController, before I want to show another UIView from a UIViewController on my storyboard. So I call this function in -viewWillAppear:

-(void)showSplashScreen{

    UIView *splashView;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];


        UINavigationController *splashVC = [self.storyboard instantiateViewControllerWithIdentifier:@"splashscreen"];

        splashView = splashVC.view;

        NSLog(@"height: %f", splashVC.view.frame.size.height);
        NSLog(@"width: %f", splashVC.view.frame.size.width);


        NSLog(@"self view height: %f", self.view.frame.size.height);
        NSLog(@"self view width: %f", self.view.frame.size.width);


    [appDelegate.window addSubview:splashView];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),dispatch_get_main_queue(), ^{
        [UIView transitionWithView:appDelegate.window
                          duration:1.00f
                           options:UIViewAnimationOptionCurveEaseOut
                        animations:^(void){
                            splashView.alpha = 0.0f;
                        }
                        completion:^(BOOL finished){
                            [splashView removeFromSuperview];
                        }];
    });

}

The UIView is always shown in portrait mode. I set everything in the storyboard to landscape mode. The app starts in landscape. Only landscape is allowed. Of course there is no function like: [self.storyboard instantiateViewControllerWithIdentifier:@"splashscreen" inPortrait: False]; Would be to easy for the developers I guess....

So, someone has an idea? (Of course I can add launch image, or I can load the UIViewController before the UISplitViewController...) but it should also work like this or not?

Upvotes: 0

Views: 314

Answers (2)

Hossam Ghareeb
Hossam Ghareeb

Reputation: 7123

This may help you:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    switch (interfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            self.transform = CGAffineTransformMakeRotation(M_PI * 270.0 / 180.0);
            break;

        case UIInterfaceOrientationLandscapeRight:
            self.transform = CGAffineTransformMakeRotation(M_PI * 90.0 / 180.0);
            break;

        case UIInterfaceOrientationPortraitUpsideDown:
            self.transform = CGAffineTransformMakeRotation(M_PI * 180.0 / 180.0);
            break;

        default:
            break;
    }
    [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

Upvotes: 0

tuvok
tuvok

Reputation: 699

-(void)showSplash{

    UIView *splashView;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName: IS_IPAD ? @"Main_iPad" : @"Main_iPhone" bundle:nil];
    UINavigationController *splashVC = [storyboard instantiateViewControllerWithIdentifier:@"splashscreen"];
    splashView = splashVC.view;


    splashView.frame = IS_IPAD ? self.splitViewController.view.bounds : appDelegate.window.bounds;

    IS_IPAD ? [self.splitViewController.view addSubview:splashView] : [appDelegate.window addSubview:splashView];


    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC),dispatch_get_main_queue(), ^{
        [UIView transitionWithView: IS_IPAD ? self.splitViewController.view : appDelegate.window
                          duration:1.00f
                           options:UIViewAnimationOptionCurveEaseOut
                        animations:^(void){
                            splashView.alpha = 0.0f;
                        }
                        completion:^(BOOL finished){
                            [splashView removeFromSuperview];
                        }];
    });

}

Upvotes: 1

Related Questions