Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Having a problem with Orientations of views and storyboards

Hello,

I have been playing with Story boards and just watched the videos and they are great by the way. But with my experimentation I was playing with the orientation of the views that I am changing between. I had to create a custom navigationViewController that I associated my NavigationView to and I have my main view when the app loads and I want that in Portrait only. Now the app starts up and it's fine it won't rotate. I have another view that I segue into and that doesn't rotate either and that is fine. now I have another view that I segue into that is actually streaming a video that I do allow for changing of orientation and that works also and when I click done on the video I actually call a segue programmatically to go back to the main view and that works woohoo and yay me LOL.

Here is the problem if they click done on the video view while it's in portrait it returns to the main view in portrait view as it should. But if I click done on the video view in landscape mode it returns to the main view but in landscape mode and the graphics all scrunched and such. If I turn to portrait it goes to portrait and locks there but I want to force it to portrait view before it returns to the main view. PLEASE HELP I have been spending hours trying to find a solutions and it looks like many many people are having this same issue but can't seem to find a solution.

Here is the code setting the view orientation perimeters in my main view controller first then I have the videoviewcontroler and finally the code that is in my custom NavigationViewController. Thank you all for any help you can provide. I'm at a loss.

-(BOOL)shouldAutorotate {
return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)self{
return UIInterfaceOrientationMaskPortrait;
}

*Here is the code I use in my video view controller*

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *movieURL = [[NSURL alloc] initWithString:@"http://yisrayl.org:1935/live/Stream1/playlist.m3u8"];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[moviePlayer.view setFrame:self.view.bounds];
[self.view addSubview:moviePlayer.view];
//some additional customizations
moviePlayer.fullscreen = YES;
moviePlayer.allowsAirPlay = YES;
moviePlayer.shouldAutoplay = YES;
moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
}

-(BOOL)shouldAutorotate
{
return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[moviePlayer.view setFrame:self.view.bounds];
}

- (void) moviePlayerPlaybackDidFinish:(id)sender {

[self performSegueWithIdentifier:@"Satellite" sender:self];

}

*Here is the code for my CustomNavigationViewController*

-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

4 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

My guess would be that your custom view controller's shouldAutorotate must return a NO because you want to support a portrait orientation for it.

Hello Amit,

Thank you so very much for responding. I have tried already tried what you have suggested. If I change to landscape mode on my video view controller and click done it returns to the main view controller but it is in landscape mode.

If I have the shouldAutorotate set to NO when I change to portrait it is then locked in landscape. If it is set to YES it will change to Portrait and lock in Portrait but the graphics are all scrunched from being in landscape.

What I'm needing it to do is some how update this call

- (void) moviePlayerPlaybackDidFinish:(id)sender {

[self performSegueWithIdentifier:@"Satellite" sender:self];

}

Tell it to change back to Portrait before it executes or on my main view controller some how check it's orientation when it gets control back and says what orientation am I in and if not in Portrait then change to portrait.

Thank you again so very much. Amit I am truly learning a lot from your videos and appreciate all your assistance.

Micahyah

I just found this code and added it to the moviePlayerPlaybackDidFinish and it works but I don't know it is legal in apples eyes when submitting an app.

- (void) moviePlayerPlaybackDidFinish:(id)sender {

if(UIDeviceOrientationIsLandscape(self.interfaceOrientation)){
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait );
}
}

[self performSegueWithIdentifier:@"Satellite" sender:self];

}
Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

That code is legal but I wonder if you could have the same effect by presenting the video view controller as a modal view controller