I have this code, i need detect is ipad launch app landscape or iphone in portrait mode only
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:#"iPhone"])
{
NSLog(#"This is iPhone");
}
else
{
NSLog(#"This is iPad");
}
This works!!! for IOS 6
-(NSUInteger)supportedInterfaceOrientations{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
return YES;
}
Related
Hi I am working on iphone app (not for ipad) which contains orientation in both protrait and landscape. My problem is sometimes orientation is not done. If i turn device to landscape mode, it is stuck in that mode only, not turn to protrait mode and vice versa.
I dont know the problem. i activated all device orientation modes in deployment info section.
Can anyone tell me the solution for this.
Thanks in advance,
Mahesh.
-(void)viewWillAppear:(BOOL)animated
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
{
// Write your orientation code here.
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// Write your orientation code here.
}
}
// For ios5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
{
// Write orientation code here for ios5
}
else if (interfaceOrientation ==UIInterfaceOrientationLandscapeLeft||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// Write orientation code here for ios5
}
return UIInterfaceOrientationMaskAll;
}
// Write code for ios6 and above
-(BOOL)shouldAutorotate
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
return YES;
}
else
return NO;
}
-(void)viewWillLayoutSubviews
{
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(interfaceOrientation == UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)
{
// Write orientation code here for ios5
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft||interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// Write orientation code here for ios5
}
}
I am developing app for ipad, Portrait images are working fine but when i switch to landscape its massed up the screen, Is there any way to manage both images or auto adjustment.
in .m file do things according to current orientation
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)orientationChanged {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if(orientation == UIInterfaceOrientationLandscapeLeft)
{
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone)
{
if (screenBounds.size.height == 568) {
//for iphone 5 LandscapeLeft
} else {
//for iphone 4 LandscapeLeft
}
}
else
{
//for ipad LandscapeLeft
}
}
else if(orientation == UIInterfaceOrientationLandscapeRight)
{
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone)
{
if (screenBounds.size.height == 568) {
// for iphone 5 LandscapeRight
} else {
// for iphone 4 LandscapeRight
}
}
else
{
//for ipad LandscapeRight
}
}
else if(orientation == UIInterfaceOrientationPortrait)
{
//do change in design when ios change
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone) {
if (screenBounds.size.height == 568) {
// for iphone 5 Portrait
} else {
// for iphone 4 Portrait
}
} else {
//for ipad Portrait
}
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do what you want in Portrait Upside Down
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPhone) {
if (screenBounds.size.height == 568) {
// for iphone 5 PortraitUpsideDown
} else {
// for iphone 4 PortraitUpsideDown
}
} else {
//for ipad PortraitUpsideDown
}
}
}
orientationChanged method called whenever your divice change orientation. than you can set image according to your new orientation. and yes you have to create your image for two orientation portrait and landscap.
There are options :-
1) if ios 6 you can use autolayout .
2) you can set frames for the uicontrols separately and use these two methods if ios 6
-(BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeLeft ;//return UIInterfaceOrientationMaskAll;
}
and after this whereever required like in ViewDidlOAd() and viewWillAppear()
do this
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication ]statusBarOrientation))
{
// change frames
}
else
{
// for landscape change frames here
}
iOS < 6.x
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation // Deprecated in iOS 6.x
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Im developing app for both iphone4 & 5. when i use shouldAutoRotation it support only for iphone4. When i use willAnimateRotationToInterfaceOrientation it supports for only iphone5. Im using both method for iphone4 &5, it working. Im going to publish my app to appStore. I got some warnings, but will work perfectly. Is apple reject app for getting warnings.
code:
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationPortrait;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
//My code here
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
//My code here
}
return YES;
}
you may have missed to close with "}" this method
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight){
//My code here
}
return YES;
}
// add:
return YES;
}
and the same for willAnimateRotationToInterfaceOrientation method...
I'm making a universal app and I was wondering if it is possible to set initial orientation to Landscape for iPad and Portrait for iPhone? Currently, I'm setting initial interface orientation in the info.plist file but it doesn't seem to have different options for iPad and iPhone . If it cannot be done through info.plist file then how to do it programmatically?
programatically you can do using following code -
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
}
else {
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
}
return NO;
}
It looks like the initial interface orientation property conflicts with the supported orientations. I found the solution here, though.
UIDevice* thisDevice = [UIDevice currentDevice];
if(thisDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight];
}
else
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
i want to run app in landscape and portrait mode,
How to check landscape and portrait mode on Appdelegate?
I try to call
- (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]) {
NSLog(#"is pad method call");
return YES;
}
else
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
}
- (BOOL)isPadPortrait
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationPortrait
|| self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
- (BOOL)isPadLandscape
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
but gives error
Property interface orientation not found on object of type appdelegate.
How can i do that?
You should check the current orientation of your iPad using. I would recommend you to check these conditions on every view and act according to the current orientation:
if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
{
//Do what you want in Landscape Left
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
//Do what you want in Landscape Right
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)
{
//Do what you want in Portrait
}
else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do what you want in Portrait Upside Down
}
I hope this helps you. :)
Feel free to contact me if you require any help.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
NSString *nibName = UIDeviceOrientationIsLandscape(orientation) ? #"Landscape" : #"Portrait";
NSLog(#"orientation is %#",nibName);
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSLog(#"Lanscapse");
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
NSLog(#"UIDeviceOrientationPortrait");
}
}
The delegate orientation is the orientation that we specify in the plist ,if we not specify in any orientation in plist it will take the portrait mode default .But in delegate you have only the key window that doesn't support the orientation you have to add the viewcontroller or any navigation controller with a view then you can call these function and they will give your the proper orientation for the device.