2

my app has been working fine until I've tried making some changes using the ios 6 SDK

The app runs in portrait mode 99% of the time. This has been constrained by only allowing portait mode to be available in the info.plist

There is one view controller which needs to be shown in landscape mode. This is achieved "manually" by simply rotating the view by 90 degrees, like so:

self.view.transform = CGAffineTransformMakeRotation(3.14159/2);

This still works fine in iOS 6.

However, this view controller has some text fields. When the user taps one it shows the keyboard. Since I've only rotated the view (and not actually changed the orientation of the device), the keyboard comes out in portrait mode, which is no good.

In previous versions of iOS, I set the orientation of the status bar to be landscape, and as a byproduct, this would set the keyboard to be landscape as well, like this:

[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];

However, this has stopped working for iOS 6.

I have read a million stack overflows trying to get this to work, but still have no luck.

Rich
  • 515
  • 1
  • 5
  • 18
  • Couldnt you simply present that single viewController modally and allow it to use landscape only? – Till Mar 15 '13 at 23:05
  • I wouldn't mind that, but my first attempt at that didnt work. The new view controller is launching in portait mode. I specified a method for preferredInterfaceOrientationForPresentation, to return landscape mode, but this method never seems to get called... – Rich Mar 15 '13 at 23:16

1 Answers1

3

Changing the keyboard orientation and transform is an difficult part and not a good solution(especially when it changes status bar orientations).

Better solutions is to allow application to support all orientations.

enter image description here

Implement the Orientation delegates inside your ViewControllers asper the rotation support.

For Supporting only Landscape

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation == UIInterfaceOrientationLandscapeLeft
    || interfaceOrientation == UIInterfaceOrientationLandscapeRight ;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

For Supporting only Portrait

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • Thanks for such a clear answer, but unfortunately for me its still not working. It just ignores the restrictions and automatically rotates when you rotate the device. Interestingly, only ONE of these methods is being called: **supportedInterfaceOrientations**. The other 2 dont ever get called. Is there something I need to do to tell the view controller that these delegate methods are available? – Rich Mar 15 '13 at 23:40
  • using ios6 right ? done the three steps ? I mean including the Support Orientation change in Targets and .plist ? – Shamsudheen TK Mar 15 '13 at 23:43
  • Yeah, if I understand you right. It is iOS 6. My 4 buttons in targets look like your screenshot above (none are dark gray), and in my info.plist, it says "Supported interface orientations (array): 0" – Rich Mar 15 '13 at 23:51
  • oh.. all seems okay now..Also do a clean and run again. if u don't mind Could u please share ur sample code via ramshadram90@gmail.com ? – Shamsudheen TK Mar 15 '13 at 23:53
  • Thanks, but it's still not working. It just doesnt call any of these methods except **supportedInterfaceOrientations**. I have seen some answers elsewhere that say you should use a category to add these methods to the view controller class. The problem is I think that will globally set all view controllers to the same orientation? – Rich Mar 16 '13 at 09:48
  • Yes that would be great, thanks. I think there might be a more fundamental problem going on here though. Its strange why none of the callbacks are ever called. – Rich Mar 16 '13 at 10:11
  • I have sent.. plz check it – Shamsudheen TK Mar 16 '13 at 10:38
  • Still not working I'm afraid, but I think I'm getting closer to the answer. I think these methods arent being called because I am using a navigation controller. There is some good info here: (http://stackoverflow.com/questions/13588325/shouldautorotate-method-not-called-in-ios6). So I have created a category, which works, but now I need some way to make a conditional statement in each view controller, so that all are portrait except one. I think I need to learn some more about categories first. – Rich Mar 17 '13 at 12:41
  • did u checked the sample app that I sent? – Shamsudheen TK Mar 17 '13 at 15:14
  • Yeah, and it worked fine. But the code didnt work in my app. From my research I think it is something to do with the way that the navigation controller is used. Those methods never get called in a view controller inside a navigation controller. However, they do get called inside the app delegate, so I put them there to lock the orientation globally. My existing code using **setStatusBarOrientation** then worked again. Thanks for all the help man! Very grateful. – Rich Mar 21 '13 at 20:51