I have a project whose rootViewController is a UINavigationController, and in my project only the view controller loading a full screen UIWebView. I need to change its orientation.
I have finished fixing the issue, but there's some trouble: When a user is in the view controller with the web view in the landscape orientation then clicks the back button, the previous view controller is also in landscape orientation. I want it to be in portrait, however. Any ideas on how to do this?
If you can read Chinese, I found this page, thanks.
implement
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
for whatever viewcontroller you require to stay in portrait.
I have try many solution, but the correct answer with working solution is:
ios 8 and 9, no need to edit info.plist.
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
possible orientation
UIInterfaceOrientationUnknown
The orientation of the device cannot be determined.
UIInterfaceOrientationPortrait
The device is in portrait mode, with the device held upright and the home button on the bottom.
UIInterfaceOrientationPortraitUpsideDown
The device is in portrait mode but upside down, with the device held upright and the home button at the top.
UIInterfaceOrientationLandscapeLeft
The device is in landscape mode, with the device held upright and the home button on the left side.
UIInterfaceOrientationLandscapeRight
The device is in landscape mode, with the device held upright and the home button on the right side.
Related
I am working on an App which is written in Objective-C. I have more screens and all screens are Landscape Left and Landscape Right orientations and these screens must not be in portrait mode. But I have got 3 different screens which should be in only Portrait mode must not be in Landscape Left or Landscape Right.
This is the code for Landscape mode for all screens-
And this is for Portrait mode for my app
These are all I did in my View Controllers and In app Plist I have added needed orientations like This Info.plist
And the app device orientation will automatically changes. like this -
.
I am pretty sure all is clear and should work as it is expected but for some reason when I use the app and lock the auto rotation and run the app it is automatically opening in Portrait mode and when I unlock the autorotation it will be in Landscape and you rotate it will be rotated to Portrait mode. I used shouldAutorate return YES because it should rotate it automatically between Landscape Left and Landscape Right so I used it also the portrait mode screen is opening in Portrait but it is autorotating when user rotates the device.
Any help would be appreciated, Please share any idea why my app is not working as expected.
Following are the steps to fix this issue:
Remove orientations from plist because it overrides whatever logic you have in your view controller.
Keep below code where you have landscape orientations:
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
For portrait mode use it as below:
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Now try locking the orientation your first view controller will appear in landscape and other one will be portrait regardless of orientation lock.
In my camera app, everything works perfectly fine orientation wise. If portrait orientation lock is not enabled on a users phone, then everything works perfectly.
However, if a user has portrait orientation lock enabled and they record sideways, then the video gets recorded in portrait mode but everything is sideways inside the video.
Is there any way to check if portrait orientation lock is enabled or any way to get around this? Thank you.
There are a couple of things. You can override the shouldAutorotate function in the view controller that you are using the camera in like so:
override func shouldAutorotate() -> Bool {
return true
}
Then you could set it to false again once they are finished with the camera so it retains their device settings. Not sure if this is the best way, but is A way.
Another possible way that you might be able to get around this is to simply always check the device orientation. The device will still detect the physical orientation of the device even if the visual orientation of the UI doesn't autorotate (https://developer.apple.com/documentation/uikit/uidevice/1620053-orientation). So the following shows the possible device orientations, as per the Apple documentation (https://developer.apple.com/documentation/uikit/uideviceorientation)
enum UIDeviceOrientation : Int {
case Unknown
case Portrait // Device oriented vertically, home button on the bottom
case PortraitUpsideDown // Device oriented vertically, home button on the top
case LandscapeLeft // Device oriented horizontally, home button on the right
case LandscapeRight // Device oriented horizontally, home button on the left
case FaceUp // Device oriented flat, face up
case FaceDown // Device oriented flat, face down
}
Knowing those you can just check if the current orientation is one of those:
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft) {
...override autorotate to true
}
These might solve your issue.
You can disable portrait orientation lock by using this method:
- (NSUInteger)lockorientation
{
return UIInterfaceOrientationMaskPortrait;
}
In the project summary, "Supported Interface Orientations" are all selected, as there is a photo gallery view in my App, which can be rotated with device. The other views are portrait only. The target devices is iPhone, and all things perform well in the iPhone. But when it runs in my iPad with landscape mode, the splash and the rootView are as following:
splash-landscape:
rootview-landscape:
What I expected look should be the same as the iPad is with portrait mode:
splash-portrait:
rootview-portrait:
The rootView is MyNavigationController, some related code is as following:
MyNavigationController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}
Please, correct your code with the following:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
It may seem odd returning YES from shouldAutorotate. The fact is, if you do return NO, then supportedInterfaceOrientations will not be called at all and your project settings will rule. You could as well remove shouldAutorotate and it should work just the same.
Reference:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller’s shouldAutorotate method returns YES.
Do you mean by showing a landscape launch screen and then in app still use portrait mode?
As far I know, iPhone-only app can't launch in landscape mode, which means giving a landscape launch screen to iPhone-only app is useless.
Check the document here at the "Providing Device-Specific Launch Images" section.
I guess what you want is make the status bar be portrait too. Unfortunately, there is no easy way to do this -- you can setup the device/interface orientation to protrait only, but it applies to the whole application. And you will need to process the orientation of all views by yourself. So, I will suggest you follow Hide status bar on launch image, hide your status bar, and use the same image in both orientations. It will make the splash screen look better.
I settings my info.plist as below:
Initial interface orientation: Landscape (left home button)
Supported interface orientations: Landscape (left home button) and Landscape (right home button)
When I started my app, the orientation device have landscape mode, but my view not rotate to this orienatation.
I use method below for rotate orientation, but view not initial in landscape mode (after I have rotate my device in this case I had needed result - view rotate in landscape mode)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
I've had a couple of issues with this in the past. Basically if you just put the initial view controller there it bugs out handling as if it was in portrait mode. The fix i found for this was adding the view controller to a navigation controller.
See this question i posted for the full explanation
I have a pretty standard iPad application that is setup to only be landscape. To affect this, I have set the initial interface orientation to landscape, and the support interface orientations to just the single landscape left home button, and also overridden shouldAutorotateToInterfaceOrientation properties to the following:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL b = UIInterfaceOrientationIsLandscape(interfaceOrientation);
return b;
}
The really odd thing is that when the app starts out, it is correct, and rotating the iPad upside down does nothing, but rotating with the home button down rotates the screen, but once rotated, it will never rotate back, so thinking this is something other than rotation settings.
Has anyone run into this before?
From the docs:
#define UIInterfaceOrientationIsLandscape(orientation) \
((orientation) == UIInterfaceOrientationLandscapeLeft || \
(orientation) == UIInterfaceOrientationLandscapeRight)
So if you want to support just ONE of the landscape rotations, this is not the way to go...