iPhone App with only portrait mode - iphone

How can i make my app to be always in portrait mode only. If i rotate iPhone it should not display app in landscape mode.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortrait);
return YES;
}
After this coding when i rotate iPhone app is displayed in landscape mode. How can i make app to be in portrait mode all the time.

Remove the method — you don't need it. An iPhone app always starts in portrait mode unless you force it to start in landscape mode. If this method is absent, it will stay in portrait mode, as it effectively means "My view won't respond to interface orientation changes."
Incidentally, this method actually starts out commented out in Xcode's view controller templates, which is the same as it not being there at all.

In xcode click left top on your project name and then right below of "TARGETS" you can set "Supported Device Orientations".

Seems for iOS 6 you need to override this method,
-(BOOL)shouldAutorotate {
return NO;
}

Related

rotate my iphone app in ipad when rotate option is enabled

I want to rotate my iphone application in ipad if I rotate the ipad
what I use in my code is :
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation == UIInterfaceOrientationMaskAll);
}
and I check both portrait and landscape:
and the plist file:
my application device family is only iphone, and appears as an iphone in ipad device
But it doesn't rotate, please correct my code above, thank you
you ask it ? ;
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
}
Put it into the code if try again.
In the .plist file you only specify your startup orientations. After that every view controller can implement shouldAutorotateToInterfaceOrientation: in which the view controller is "asked" if a rotation to another orientation is acceptable. In the standard template for iPad apps this always returns YES and thus allows all orientations. In your case you might only return YES when the given orientation is UIInterfaceOrientationLandscapeLeft, although you should have a look if you can support both landscape orientations as Apple human interface guidelines strongly suggest to at least support both landscape orientations if one is supported.
Note that every view controller in your app has to specify its own orientations as it may make sense to have some views more restricted than others.
For further information on this have a look at:
Supporting orrientations for iPad apps: http://developer.apple.com/library/ios/#qa/qa1689/_index.html
Why won't my UIViewController rotate with the device: http://developer.apple.com/library/ios/#qa/qa1688/_index.html
UIViewController class reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Force landscape mode at the beginning of app, but allow later changes in orientation

I am planning to allow user to rotate the devices however during launch, i want the app start from landscape mode. May i know how can i do that?
Here is my code now for the orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
You need to check the orientation of the status bar.
Please check the following links :-
Force iOS app to launch in landscape mode
Modal View Controller force Landscape orientation in iOS 6
Force Landscape Orientation on iOS 6 in Objective-C
In the Project settings/target settings (the blue icon towards the top of the navigation bar on the left, in xcode), there are settings for specifying the orientation. You'll see 4 images, each of which can be selected or deselected.
Select only the one that you want initially for your app, and then you can use the shouldAutorotate and supportedInterfaceOrientation methods to allow or disallow certain orientations.
Supported interface orientations are also specified in the plist. Makes sure to keep the initial orientation for the app at the top of that supoortedInterfaceOrientations list in the plist file.

How can I disable landscape orientation?

I am making an iPhone app and I need it to be in portrait mode, so if the user moves the device sideways, it does not automatically rotate. How can I do this?
To disable orientations for a particular View Controller, you should now override supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// Return the orientation you'd prefer - this is what it launches to. The
// user can still rotate. You don't have to implement this method, in which
// case it launches in the current orientation
return UIInterfaceOrientationPortrait;
}
If you're targeting something older than iOS 6, you want the shouldAutorotateToInterfaceOrientation: method. By changing when it returns yes, you'll determine if it will rotate to said orientation. This will only allow the normal portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
// Use this to allow upside down as well
//return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Note that shouldAutorotateToInterfaceOrientation: has been deprecated in iOS 6.0.
Xcode 5 and above
Click your project in the Project Navigator in the left sidebar to open the project settings
Go to the General tab.
Uncheck the options you don't want in the Deployment Info section, under Device Orientation
Xcode 4 and below
For those who missed it: you can use the project settings screen to fix orientations throughout the app (no need to override methods in every controller):
It's as simple as toggling the supported interface orientations. You can find by clicking on your Project in the left panel > the app target > Summary tab.
Most simple solution separate for iPhone and iPad (Universal) - its remove unnecessary orientation in the info.plist file or Project -> Info -> Custom iOS Target Properties.
Just add or remove orientation item from list:
Supported interface orientation for iPhone
Supported interface orientations (iPad) for iPad
In Xcode 13.3.1, simply unchecking undesired orientations does not prevent an app from supporting all rotations. It is necessary to enter the Build Settings tab and manually remove any orientations from the following fields that you do not wish to support:
In my case, my app will now only support portrait orientation.
If you want to disable landscape orientation for both iPhone and iPad.
Go to Targets and Go to the General tab. See the below screen and deselect landscape left and landscape right.
Here in this case only iPhone landscape mode will be disabled not for iPad. For iPad all modes are anabled. If you want select device option from Universal to iPad. It will looks like this. See below screen.
Now you need to deselect all modes except Portrait for iPad. See below screenshot.
Now you successfully disabled all modes except Portrait for all devices.
If you created a new Xcode 13.3 project and unchecked unnecessary orientation checkmarks in the Project > General > Deployment and it didn't help. Check the Target > Build Settings - there are 2 rows which override global settings.
Swift 3
If you have a navigationController, subclass it like this (for portrait only):
class CustomNavigationViewController: UINavigationController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return UIInterfaceOrientation.portrait
}
}
Removing the method shouldAutorotateToInterfaceOrientation from your class entirely also works. If you don't plan on rotating then it makes no sense to have the method in your class, the less code the better, keeps things clean.
Xcode 8, Xcode 9, Xcode 10 and above
Also, make changes in Info.plist file
I've had the same problem on Xcode 13.0 even though I set the device orientation only Portrait.
Adding these 2 lines to Info.plist solved my problem.

how to lock/unlock orientation lock of iphone programmatically?

we know that these simple steps to lock/unlock the orientation of your iPhone 3G:
From any screen, double-tap the home button
Scroll as far left as you can on the multitasking dock
The first symbol to the left will be a circular arrow
Select this to either lock or unlock the orientation of your iPhone 3G
But how we can do this programatically ?
Are you asking if you can do this for your app or lock the orientation for the device itself? Seems to me you're asking for the latter and I would have to ask why you want to do that. It's not possible to lock the orientation for the device, because that way it would be locked in portrait mode for other apps as well.
You can however only support the orientations you want yourself. A lot of apps only support portrait mode and games generally support landscape only.
You can set the supported device orientations of your app in XCode. At the viewcontroller.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Assuming you want to support both landscape orientations.
You can't do it programmatically -- it would be plain wrong for an app to change a setting that affects everything else.
In your own app, you can restrict the supported orientations by setting UISupportedInterfaceOrientations in your info.plist (see doc here). You can also restrict orientation per view through shouldAutorotateToInterfaceOrientation (see doc here)

Launching application in landscape orientation for IPad

Facing one issue with launching application in landscape orientation for IPad.
I have developed IPhone application which later I ported to IPad.
I have made setting regarding orientation in info.plist
[ UISupportedInterfaceOrientations~ipad ] to support all orientation UIInterfaceOrientationPortrait , UIInterfaceOrientationPortraitUpsideDown , UIInterfaceOrientationLandscapeLeft , UIInterfaceOrientationLandscapeRight.
but when I start IPad application in the landscape mode, it always start in the potrait mode.
Along this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{ return YES; }
help me, if I am missing something with this..
Thanks,
Sagar
here's something I also discovered: setting the initial interface orientation in your info.plist is being ignored if you have Supported interface orientations set up with another orientation in the first slot! Put your initial orientation there as well - and the simulator will launch correctly, as will the app. this drove me nuts for a long time!
Put UISupportedInterfaceOrientations into your -Info.plist, with a setting for each orientation you support. This is used to see which orientation the app can start in. From there onwards it will ask your view controllers.
Sagar - I had the same issue but was able to resolve it.
Like yours, my app started as an iPhone app which I "upgraded" to a Universal app using the XCode wizard. I noticed that when running on the actual iPad, starting in landscape, the app would start in Portrait, then maybe rotate to Landscape. On the simulator, starting in landscape, the app would start in Landscape, then the simulator would rotate to Portrait.
On the iPad, my app is a split-view app with TabBarControllers on left and right. Each tab is a view controller that returns YES to shouldAutoRotateToInterfaceOrientation.
I noticed that a brand-new wizard-generated, simple-case with a splitviewcontroller, Universal app didn't have this problem.
The difference I found between my app and the simple-case was that I wasn't adding my splitview-controller's view to the app window in applicationDidFinishLaunchingWithOptions. Instead I was showing a "loading" view at this stage, then later when an initialization thread completed I'd add my splitviewcontroller's view (and hide the "loading" view).
When I added my splitviewcontroller's view to the app window during the call to applicationDidFinishLaunchingWithOptions everything started working fine.
There must be some magic that happens on return from applicationDidFinishLaunchingWithOptions???
Is your app similar to mine in that it isn't adding the main view controller's view to the window during applicationDidFinishLaunchingWithOptions?
As pointed out in a number of posts, you must set up the info.plist with both the supported and the initial interface orientations. However, the bigger issue is when does the initial orientation become effective? The answer is NOT when your view controller receives the "viewDidLoad" message. I found that on the iPad-1, running iOS 5.0, the requested initial orientation becomes effective only after several "shouldAutorotateToInterfaceOrientation"
messages are received.(This message passes the UIInterfaceOrientation parameter to the receiver.) Furthermore, even if the orientation says it is in Landscape mode, it may not be! The only way I found to be sure that the view is in Landscape mode is to test that the view height is less than the view width.
The strategy that worked for me was to lay out the subViews I wanted when the "viewDidLoad" message was received but to delay actually adding those subViews to the view until the controller received a valid "shouldAutorotate.." message with the orientation set to Landscape mode. The code looks something like:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
// N.B. Even when the interface orientation indicates landscape mode
// this may not really be true. So we insure this is so by testing
// that the height of the view is less than the width
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
CGRect viewBounds = [[self view] bounds];
if ( viewBounds.size.height < viewBounds.size.width )
[self addMySubViews];
return YES;
}
else
return NO;
}
Apple has just released iOS 5.1, so this behavior may have changed. But I expect the code that is here should still work.