i am check device and interface orientation programetically but every time it goes to landscape Orientation even in portraite mode also
Here is my code please give me Solution:-
if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeLeft||[[UIDevice currentDevice] orientation]== UIDeviceOrientationLandscapeRight || [self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight)
{
}
else
{
}
Always use
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
//orientation is portrait
}
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
//orientation is landscape
}
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) )
{
//Portrait coding
}
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || [[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft))
{
//Landscape coding
}
Related
I am writing app for iPhone. It works fine but in iPad it does not scale correctly. I have landscape view and I did it programatically. Then i am doing push. Then it shows this:
i want it to rotate and fullscreen. Please help me. Thanks in advance.
you can check and implement Orientation in ios6 or ios5 like bellow code And information Put Method in Each Class:
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
and check every time in ViewWillApear device Oriantation like:-
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
UPDATE
likely people use checking deviceorientation like below way in putting this line in to ViewWillApear:-
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
and
-(void)deviceRotated:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your stuff for landscap
}
else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do your stuff for potrait
}
}
IN IOS5 only landscap you can do like bellow:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
else
{
return NO;
}
}
if you wish to support all oriantation you need to just return YES like:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I am showing a custom UILabel in my appdelegate class when a push notification comes. It is working fine in Portrait mode but when i rotate my device to landscape mode, label is still showing in Portrait mode. How can i fix it. I have implement rotation method. But it did not worked. Thanks in advance.
My code is :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
CGRect frame=[[UIApplication sharedApplication] statusBarFrame];
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft
|| [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
backgroundImageView=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 32)];
} else {
backgroundImageView=[[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height, 320, 44)];
NSLog(#"portrait");
}
[backgroundImageView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"alert.png"]]];
backgroundImageView.userInteractionEnabled=YES;
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(click:)];
[backgroundImageView addGestureRecognizer:tgr];
[self.window addSubview:backgroundImageView];
}![enter image description here][1]
I have done the similar kind of thing using the following code...
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustUrLableViewForOrientation:toInterfaceOrientation];
}
-(void) adjustUrLableViewForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Ur lable portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Ur lable for landscape view
}
}
Also you can look into this changing the lable position based on the orientation.
UiWindow subviews dont rotate by themselves..
you need to check for device rotation changes like this
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceOrientationDidChange:)
name:UIDeviceOrientationDidChangeNotification object:nil];
and then in the method..rotate your label manually based on current orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown );
}
Add this...Hope this helps..
Or add this:
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationPortrait || UIInterfaceOrientationLandscapeLeft || UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationPortraitUpsideDown ;
}
[backgroundImageView setTransform:CGAffineTransformMakeRotation(M_PI/2.0)];
Should work for this case
I'm using ios6 version for generating a new Ipad app. In my app i'm creating a split view. That split view has to always in a landscape mode. App is working in ipad 6.0 simulator. But not working in ipad 5.0 simulator. I want to run the app both ipad 6.0 and ipad 5.0.
I'm using this code
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL) shouldAutorotate {
return NO;
}
you can do in ios6 like this way:-
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
and check every time in ViewWillApear device Oriantation like:-
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
UPDATE
likely people use checking deviceorientation like below way in putting this line in to ViewWillApear:-
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
and
-(void)deviceRotated:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your stuff for landscap
}
else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do your stuff for potrait
}
}
IN IOS5 only landscap you can do like bellow:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
else
{
return NO;
}
}
if you wish to support all oriantation you need to just return YES like:-
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
In iOS 5.0 you should use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationMaskLandscape;
}
I'm currently in the midst of creating an application with six views, each view being dedicated to a combination of device type and orientation. In case you don't understand what I mean, I'll label all six combinations:
iPhone4_(320x480) & Portrait
iPhone4_(320x480) & Landscape
iPhone5_(320x568) & Portrait
iPhone5_(320x568) & Landscape
iPad_(768x1024) & Portrait
iPad_(768x1024) & Landscape
I know it seems a bit silly to go through all of this trouble, but it is kind of required in this situation. Anyway, I've been trying to attempt a combination between Apple's solution from the Programming Guide and a method given from an online tutorial found at TheAppCodeBlog.
ViewController.m --> ViewDidLoad method
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
ViewController.m --> orientationChanged method
- (void) orientationChanged:(NSNotification *)object
{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 480.0)
{
self.view = self.portrait4View;
}
else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
{
self.view = self.landscape4View;
}
else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 568.0)
{
self.view = self.portrait5View;
}
else if ((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 568.0)
{
self.view = self.landscape5View;
}
else if (deviceOrientation == UIInterfaceOrientationPortrait && [UIScreen mainScreen].bounds.size.height == 1024.0)
{
self.view = self.portraitPadView;
}
else
{
self.view = self.landscapePadView;
}
}
Interface Builder setup for each view:
I'm also receiving a some errors for Expected identifier:
Update
Whenever I launch the app, I just get a black screen. Here is what my AppDelegate.m looks like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_Portrait5" bundle:nil];
}
else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 480.0) {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_Portrait4" bundle:nil];
}
else {
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController_PortraitPad" bundle:nil];
}
}
Your error related to Expected Identifier is because your parens are wrong. Your if condition should be (and I see the same error in a couple of the other conditions as well):
else if (((deviceOrientation == UIInterfaceOrientationLandscapeLeft) || (deviceOrientation == UIInterfaceOrientationLandscapeRight)) && [UIScreen mainScreen].bounds.size.height == 480.0)
iPad runs fine for portrait, but not working for Landscape,
I use this code
- (BOOL) isPad{
#ifdef UI_USER_INTERFACE_IDIOM
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#else
return NO;
#endif
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([self isPad]) {
return YES;
}
else
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
- (BOOL)isPadLandscape
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
- (BOOL)isPadPortrait
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationPortrait
|| self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([self isPadPortrait])
{
[imageView setFrame:CGRectMake(0, -50, 768, 1024)];
}
else if ([self isPadLandscape])
{
[imageView setFrame:CGRectMake(0, 0, 1024, 768)];
}
}
i try to call -(BOOL)isPadLandscape method during debug,but that method not call,
What could be wrong?
Don't forget to set property Supported interface orientations (iPad) in your Info.plist file for appropriate supported positions.
Or try this:
- (BOOL) isPad{
return [[UIDevice currentDevice].model hasPrefix:#"iPad"];
}