restrict upside down in iPad - objective-c

I disabled the upside down orientation in iPhone device though TARGET>GENERAL>DEPLOYMENT->DEVICE ORIENTATION but its not disabled in iPad.Please tell how to disable upside down orienatation in ipad

Try this
-(NSUInteger)supportedInterfaceOrientations {
UIViewController *topVC = self.topViewController;
return topVC.supportedInterfaceOrientations;
}
Hope it helps.

Related

Complex Multi-Device Orientation Handling

I have a universal application running both on iPads and iPhones. The application starts with a .xib file, built in interface builder, which acts as the launch image. Once the app launched, it switches to the appropriate view controller based on device size set in the app delegate:
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if (screenSize.height <= 568.0f) {
// iPhone 4, 4S, 5, 5C, 5S, SE
self.viewController = [[iPhoneSmallViewController alloc] init];
} else {
// All other iPhones
self.viewController = [[iPhoneLargeViewController alloc] init];
}
} else {
// All iPad models
self.viewController = [[iPadViewController alloc] init];
}
The iPad view controller supports all interface orientations (set in app targets/main setup page), but on iPhones I only allow portrait mode restricted in the view controller as such:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
I have two problems with this method:
If the iPhone is held horizontally, the app still loads in portrait mode (as per the restrictions, which is all good) but all measurements are in landscape upon initialization. UI elements stick out on the side because they were measured for a landscape view but placed on a portrait.
I use the window's size to set up everything inside the view by initializing the following variable in the ViewDidLoad method:
windowSize = [[UIScreen mainScreen] bounds].size;
Tt gives me landscape dimensions in phone is held horizontally, even though landscape mode is not allowed.
If the app loads with landscape measurements initially, all my sorting of screen sizes in the app delegate are off since I identify iPhone models by measuring screen width that is only good in portrait mode.
Question: does anyone have a way to handle this complex problem in an elegant and simple way?
Some additional info: I use Xcode 10, support all the way back to iOS9 and do everything programmatically in Objective C.
p.s: I think this method worked before but not any more in iOS 12. But I could be wrong...
Edit: I provide an image of what I want to accomplish, and all help would be greatly appreciated. As I said, this has worked before (the app is quite old), but in recent iOS releases got increasingly buggy and desperately needs a cleanup, which is what I need help with.
One thing that might solve my problem, is if I could somehow restrict interface orientations based on device type in the launchScreen.xib, as I believe that is what causes the faulty behavior on iPhones.
Maybe this SO will be helpful,
They are detecting the device orientation and then rotating the view, look at first answer:
Change orientation programmatically with button - iOS
UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
A better way to detect device is explained here:
iOS detect if user is on an iPad
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
return YES; /* Device is iPad */
}
I've been experimenting with this for days, and worked out a solution. Although this is probably not the most elegant way to do it, so if anyone has a better solution, please feel free to post it.
It is important to allow all interface orientations in the info.plist because I was unable to restrict them based on device size in the launchScreen.xib.
Create the universal launch screen that supports both iPads and iPhones. Because all interface orientations are allowed in the info.plist, this will have no restrictions.
Below is my current method in the app delegate. This is not the best way to do identifying the smaller iPhones (which I need for reasons... :), but because of the size differences, this works quite well.
At this point, the phone can be in any of the four interface orientations set in the info.plist, but because only the small handsets have a 320-width it is easy to catch it:
// Get screen size
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// Determine device based on screen dimensions
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ( (screenSize.height == 320.0f) || (screenSize.width == 320.0f) ){
// iPhone 4, 4S, 5, 5C, 5S, SE
self.viewController = [[iPhoneSmallViewController alloc] init];
} else {
// iPhone 6, 6S, 6P, 6SP, 7, 7P, 8, 8P X, XS, XM, XR
self.viewController = [[iPhoneLargeViewController alloc] init]; //Same as previous
}
} else {
// All iPad models
self.viewController = [[iPadViewController alloc] init];
}
Restrict interface orientations in the iPhone view controllers (all other view controllers will inherit the ones we set in the info.plist).
Do it like so:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait);
}
There is another trick we need to do for the above to work though. When the window loads for the first time, it will not take into consideration the restriction we added to the view controller. That means, if we do our setup in the viewDidLoad method, we will receive landscape screen dimensions if the device is held horizontally (even though this orientation is not allowed). Restriction will be applied once the viewDidLoad method has concluded.
Therefore, to prevent buggy behavior, you need to create a separate method in which you do your setup (such as postViewDidLoad) and call it once the real viewDidLoad had concluded:
- (void) viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(postViewDidLoad) withObject:nil afterDelay:0.0f];
}
In this method, you will get access the real screen size based on your restrictions you set in the supportedInterfaceOrientations method.
And that is basically it. If you have multiple views, all with different restrictions, just follow steps 4 and 5 in each of them to properly setup your workflow.

Autoresizing UINavigationBar in AnnotationView

In my app I have a TabbarController. One of the tabs opens a View which starts the iPhone camera. This CameraView is part of a SDK which is developed by another company. So I can't modify the class of this CameraView.
My problem is that I have to implement a UINavigationBar in this CameraView. I've solved this through a annotationView (this navigationbar is not handled by a navigationcontroller):
[self.annotationView addSubview:navbar];
All works well except the autoresizing matter. If I turn the phone to landscape mode the navigationbar is too short.
I have already tried to set Autoresizingmask but that doesn't help.
Do you have any ideas how I can force the navigationbar to autoresize?
Thanks in advance
Try to adjust the frame of the navigationbar manually, instead of using autorotation.
-willRotateFromInterfaceOrientation:(UIInterfaceOrientation)fio toInterfaceOrientation:(UIInterfaceOrientation)tio
{
if(tio == UIInterfaceOrientationLandscape)
//set navbar frame
else
//...
}

iPhone navigation bar rotation issue when coming back from background

Ok, so after weeks of headache, I've given up and decided to turn to expert resource online!
Basically, I am trying to get my iphone application to view in a forced landscape mode (to be precise, for a graph) for just 1 view controller in otherwise a portrait-orientation dominated navigation-bar application.
I got everything working the way it should, EXCEPT when I put the application into background and return, the application returns with the navigation bar moved to where it usually sits in Portrait orientation mode but rotated 90', whilst every others like status bar, the main view are all still in landscape mode.
I've tried to manually correct the navigation bar orientation afterwards, but cocoa seems to ignore this bit of code. It's almost as if the self.view.transform doesn't work when the application returns from background mode. Any suggestions?
LandscapeViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
-(void)viewWillAppear:(BOOL)animated {
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
This image shows you can see what I mean
http://i.stack.imgur.com/Qx1Si.png
Have you tried this?
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
You should not always return NO from shouldAutorotateToInterfaceOrientation: but return YES for the orientation you support. The iOS then handles the rest.

How do I prevent an iPhone background image from rotating when the device orientation changes?

I've set shouldAutorotateToInterfaceOrientation to return to YES for orientation, but I noticed as the iPhone rotates the background rotates as well which is a UIImageView. How do I have the background fixed so it doesn't rotate?
this seemed to do the trick. i got everything to be fixed in landscape mode.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Landscape only View in Cocoa Touch

Is there to way to fix a view's orientation in a landscape position, regardless of the device's current position?
To be more specific, I have a UIWebView in a UINavigationController that should always show its content as if the device were rotated 90 degress counterclockwise. I only want the web view to be restricted to this orientation; every other view in the app should behave normally.
Yes. In your info.plist file, define:
UIInterfaceOrientation UIInterfaceOrientationLandscapeRight
EDIT:
The above changes the orientation for the whole app, and you say you don't want that. Can the user move the phone to achieve the orientation you need?
If you don't want to rely on the user to change the phone to that orientation, try:
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
In the controller for the view, implement shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return UIInterfaceOrientationIsLandscape(orientation);
}
Or if you only want to allow Left Landscape (the default for the Youtube app, for example)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return orientation == UIInterfaceOrientationLandscapeRight;
}