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...
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
}
}
Sorry if this question is duplicate, but I can't find the same solution.
In iOS6, if I want to set default orientation for one UIViewController, I just use:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscapeLeft;
}
But how to do same in iOS5, I test both:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);//not default
}
and
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationMaskLandscapeLeft);// = return NO;
}
But the default orientation always Portrait.Any suggestion?
call the below method where you want to check the status of orientation.happy coding :-)
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self CheckForViewForOrientation:toInterfaceOrientation];
}
- (void) CheckForViewForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Ur lable portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Ur lable for landscape view
}
}
If you want to achieve this for whole application, change supported orientation UISupportedInterfaceOrientations in app.plist file
- (BOOL)shouldAutorotate {
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation==UIInterfaceOrientationPortrait) {
// do some
}
return YES;
}
Include both the methods to support both 5 and 6
You need to provide
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
for that single View controller.
I want to lock orientation to Portrait if device is iPhone, and allow all orientations if the device is iPad.
I have this code, but it doesn't lock iPhone in Portrait mode:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
return NO;
}
else
{
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
}
return NO;
}
What is the problem?
if you will navigate the view then force orientation will not work but if you will present your view then it will work very well and it works for me. In my project all screens are in portrait mode only but only one screen is in landscape mode.
- (IBAction)startButtonClicked:(id)sender {
CTFailure_RemedyGameViewController *remedyGameController = [[CTFailure_RemedyGameViewController alloc]initWithNibName:#"CTFailure_RemedyGameViewController" bundle:nil];
[self presentModalViewController:remedyGameController animated:NO];
[remedyGameController release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#end
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
else{
return YES;
}
}
Try this. Hope this helps.
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.