Load another .xib when orientation in iOS - ios

I used below code to implement that but it not work. Please give me some advices. Thanks much.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
orientationChanged() function:
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
adjustViewsForOrientation() function:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"load portrait view");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardViewController" owner:self options:nil];
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(#"load landscapeview");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardLandscapeView" owner:self options:nil];
}
}
When run it always show DashBoardViewController.xib. In lanscape, it still shows DashBoardViewController.xib. I don't know why.

This is because your not actually setting the view.
You need to do something like:
self.view = [[NSBundle mainBundle] loadNibNamed:#"DashBoardLandscapeView" owner:self options:nil] objectAtIndex:0];
This will load the view from the Xib file, and set the current view to that view.
Don't forget to add the objectAtIndex:0 as you see in the code.

Use willRotateToInterfaceOrientation: method instead of notifications
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
{
NSLog(#"load landscapeview");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardLandscapeView" owner:self options:nil];
//[self viewDidLoad];
}
else
{
NSLog(#"load portrait view");
[[NSBundle mainBundle] loadNibNamed:#"DashBoardViewController" owner:self options:nil];
//[self viewDidLoad];
}
}
Maybe you have to uncoment [self viewDidLoad] if you have som logic there.

Related

Send Action on rotation of device iOS

here is what I'm trying to achieve, it would be to send an action on rotation of the device...
the point is i'm having a 360 player, I would like the video to start only once the device is on landscape. If it's in portrait i'd like to display a message a saying "rotate your device in landscape to start", and when the user rotate the device, the video play.
below is my code so far:
- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:#"HTY360PlayerVC" bundle:nil url:url];
[videoController VRMode:true];
if (![[self presentedViewController] isBeingDismissed]) {
[self presentViewController:videoController animated:YES completion:nil];
}
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
The problem of this code is than it trigger only on TouchUp Inside in the Sent Event . . . anyway to have the action starting on rotation ?
Thanks for all your help !
EDIT---
TRYIED THE FOLLOWING ASWELL:
- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:path];
Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:#"HTY360PlayerVC" bundle:nil url:url];
[videoController VRMode:true];
if (![[self presentedViewController] isBeingDismissed]) {
[self presentViewController:videoController animated:YES completion:nil];
}
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
Have you tried
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
Try put your code inside:
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
}
and write this methods in your UIViewController. This method is called when the device (interface really) is rotated; if you want do something before the rotation of interface, use willRotateFromInterfaceOrientation

Changing XIB on rotating doesn't work correctly on iOS 8

I have trouble with my app running on iOS 8. When i start app in portrait mode, its work correctly, but when start it in landscape mode i have things like this https://www.youtube.com/watch?v=Ds-eOfxhPGc&list=UUWJmewTedt8KvB5kjTam8FA
I use 2 XIB and change it on rotating by this code. Its work great on iOS 7, but doesn't work correctly on iOS 8
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
{
[[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:#"%#-landscape", NSStringFromClass([self class])]
owner: self
options: nil];
[self viewDidLoad];
}
else
{
[[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:#"%#", NSStringFromClass([self class])]
owner: self
options: nil];
[self viewDidLoad];
}
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
[[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:#"%#-landscape", NSStringFromClass([self class])]
owner: self
options: nil];
[self viewDidLoad];
} else if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
[[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:#"%#", NSStringFromClass([self class])]
owner: self
options: nil];
[self viewDidLoad];
}
}
-willRotateToInterfaceOrientation:duration: is deprecated in iOS 8. Use -viewWillTransitionToSize:withTransitionCoordinator: instead. If you need to get the orientation, use [[UIApplication sharedApplication] statusBarOrientation].

Load different .xib for different orientation

In my application, i want to load different .xib for different orientation i.e. one for portrait , one for landscape
I searched it and found solutions too, but it isn't working for me. Anybody please help me out with this.
Here is my code:-
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}
- (void) orientationChanged:(NSNotification *)note {
UIDevice * device = note.object;
NSArray *array;
UIView *mainView;
if (device.orientation == UIDeviceOrientationPortrait || device.orientation == UIDeviceOrientationPortraitUpsideDown) {
array = [[NSBundle mainBundle] loadNibNamed:#"NewViewController" owner:self options:nil];
mainView = [array objectAtIndex:0];
NSLog(#"Portrait");
} else {
array = [[NSBundle mainBundle] loadNibNamed:#"Landscape" owner:self options:nil];
mainView = [array objectAtIndex:0];
NSLog(#"Landscape");
}
}
Thanks in advance!

MPMoviePlayerController.view isn't removed from superview

here is my code
- (void)viewDidLoad
{
...
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"movie" ofType:#"mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:_moviePlayerController.view];
_moviePlayerController.fullscreen = YES;
_moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
_moviePlayerController.controlStyle=MPMovieControlStyleDefault;
[_moviePlayerController play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieIsOver:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)movieIsOver:(NSNotification *)notification
{
NSLog(#"movie is over");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.moviePlayerController.view removeFromSuperview];//moviePlayerController is MPMoviePlayerController
}
When the movie plays to the end, I can't see "movie is over" log and the moviePlayerController.view isn't removed. I don't know why.
EDIT:
MPMoviePlayerPlaybackDidFinishNotification works well.I see the "movie is over" log.The problem is moviePlayerController.view isn't removed.
I found out the solution:add
_moviePlayerController.fullscreen = NO;
before removing view from superview
Instead of adding _moviePlayerController.view as subview
[self.view addSubview:_moviePlayerController.view];
you can present _moviePlayerController just like this :
[self presentMoviePlayerViewControllerAnimated: _moviePlayerController];
and in movieIsOver method you simply dismiss
[self dismissMoviePlayerViewControllerAnimated];
I hope its help

MPMoviePlayer not removing from superview

I am using the following code to play video files in MPMoviePlayerController
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"one" ofType:#"mp4"];
NSURL* url = [NSURL fileURLWithPath:filePath];
_movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
[_movie.view setFrame:self.view.bounds];
[self.view addSubview:_movie.view];
_movie.fullscreen=YES;
_movie.controlStyle=MPMovieControlStyleFullscreen;
[_movie prepareToPlay];
[_movie play];
and
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(close:)name:MPMoviePlayerPlaybackDidFinishNotification object:_movie];
and
- (void) close:(NSNotification *)notification {
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if(reason == MPMoviePlaybackStateStopped) {
NSLog(#"Stop");
}
else if (reason == MPMovieFinishReasonPlaybackEnded) {
NSLog(#"Playback Ended ");
}
else if (reason == MPMovieFinishReasonUserExited) {
NSLog(#"Exited");
[_movie.view removeFromSuperview];
}
else if (reason == MPMovieFinishReasonPlaybackError) {
//error
NSLog(#"Error");
}
}
I am able to get the Notification , and the Movieplayer is not removing from the superview.
What could be the problem ??
Try this follow instructions:
when I listen to MPMoviePlayerWillExitFullscreenNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:_movie];
And selector method:
-(void)doneButtonClick:(NSNotification*)aNotification{
[_movie.view removeFromSuperview];
}
(or)
Better way to use mpmovieplayerviewcontroller in below tutorial
http://mobiledevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

Resources