0

I need to know if there is a way to tell a iOS7 device to set a views orientation without the device being rotated. Some way in code to trigger the device to calling the code that tells it which way to display the view.

If the device is in landscape and remains held in landscape orientation while a certain change happens I want to force a change to show the view in portrait orientation, at which point the user would need to turn the device to look at it properly. I'll explain why below

Looking at my app might make my description clearer - it is free to download

I have a number of view controllers (embedded in navigationControllers) and only one of them needs to be rotated into landscape and then only under certain conditions.

Solutions here on StackOverflow seem to be to make a category on UINavigationController giving it shouldAutorotate and supportedInterfaceOrientations methods and then use those methods in the individual viewControllers to block or allow rotations.

This has worked for me .... however

On the one view controller I wish to rotate , I don't want it to rotate all the time.

This view controller is the diveSiteDetailsController, (if you have downloaded the app you need to select dive sites on the first page then click the '+' to see it). It has a UISegmentedController and 4 subviews (3 tableviews and 1 other UIView). The current version on the App Store works fine now i've solved this - but looking at it may help you understand my issue better).

On diveSiteDetailViewController the UISegmentedController is used to switch between the 4 subviews.

All the subviews are used to enter data about the same dive site but as there is a lot of potential data, I have broken it into logical chucks each of which is a subview - location, data (depths,currents, visibility), type of environment and notes.

The .hidden property of each subview is used to make them appear and disappear.

I only want the second subview to rotate (the data view - it has some sliders on it that are easier to work with if in landscape).

restricting this rotation is easy - iI achieved it like this

- (NSUInteger)supportedInterfaceOrientations{
if (self.dsDataRangeSlidingTV.hidden) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}

Now the view will only rotate to landscape when the data table view is displayed.

However, once in landscape, if I chose a different subview with the UISegmentedController then they are, obviously, shown in landscape also as the iOS device hasn't done a rotation. This is the situation I am trying to avoid.

Rotating the iOS device will return those views to portrait as expected but i need to trigger the device to to reevaluate its display when I use the UISegmentedController to switch from the data subview to another subview and its that triggering that I don't know how to do.

any suggestions greatly received.

SimonTheDiver
  • 1,158
  • 1
  • 11
  • 24

1 Answers1

0

Heres a workaround that is working for me

I've added the following few lines to the end of my method that responds to the UISegmentedControl being tapped.

UIViewController *aDummyController = [[UIViewController alloc]init];
[self presentViewController:aDummyController animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];

adding a new viewController and popping it off triggers the rotation . This is a kludgey way of achieving what I wanted.

I found the solution in this post

Is there a documented way to set the iPhone orientation?

all credit to Josh who although not the accepted answer is the one that 99 people currently have up voted.

I still have a bug in that, if I were holding the device in landscape (although the display is portrait view) whilst on the screen that segues into the diveSiteDetailsController then the initial view the diveSiteDetailsController display will be in landscape.

To get around this I created a Bool property called notThisTime on the diveSiteDetailsController and set it to true in the prepareFor Segue on the viewController that called it.

i then did changed supportedInterfaceOrientation to

- (NSUInteger)supportedInterfaceOrientations

{//   DLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));

if (self.notThisTime){
    return UIInterfaceOrientationMaskPortrait;
}
if (!self.dsDataRangeSlidingTV.hidden) {

return UIInterfaceOrientationMaskAllButUpsideDown;

}

then at the end of the ViewDidLoad method I added

self.notThisTime = NO; 

I would still love to hear from anyone with a suggestion how better to handle this. pushing and popping a dummy view to get the iPhone to do an orientation check seems like a work around for something that should just be available as a standard method call.

One final Note - the iOS simulator does not like this - you need to check on the device - it sometimes tries to draw the iPhone container in landscape while the screen is drawn vertically - however it does work fine on the iPhone

Community
  • 1
  • 1
SimonTheDiver
  • 1,158
  • 1
  • 11
  • 24