Load different .xib for different orientation - ios

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!

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 another .xib when orientation in 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.

NSNotification Received But Method Is Not Called

I am posting notification from UIViewController object to UIView object. Notification is called but its method "playSound" is not called.
Below is my code and Thanks in Advance :
#interface SettingsViewController : UIViewController <MFMailComposeViewControllerDelegate>
{
MFMailComposeViewController *mailComposer;
}
- (IBAction)btnSoundClicked:(id)sender {
if(self.btnSound.on)
{
NSLog(#"Swith On");
[[NSNotificationCenter defaultCenter] postNotificationName:#"soundStatus" object:self.btnSound];
}
else
{
NSLog(#"Switch Off");
}
}
#interface IAPuzzleBoardView : UIView <AVAudioPlayerDelegate> {
}
- (void)movingThisTile:(CGPoint)tilePoint {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playSound:)
name:#"soundStatus"
object:nil];
}
-(void) playSound : (NSNotification *) notification
{
if ([[notification name] isEqualToString:#"soundStatus"])
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"move" ofType:#"wav"];
theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}
}

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