Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone force rotation

Tags:

iphone

I have been reading a ton on rotation, but not finding a solution to my query. Here goes:

I have a portrait application with a tabbar and hidden navigation controller in my tab. At a point in the app, the next view must be landscape.

The XIB layout has been done in landscape, so I want to bring up the xib without any translation or pixel moving code. (not thinking this is required) I have tried just pushing the view (remains in portrait), shifting the view using various methods (non seem to line thing up properly).

Is there a way to tell the view that it is already laid out for landscape prior to it being opened?

Thanks in advance!

like image 820
Rob Bonner Avatar asked Jan 22 '10 14:01

Rob Bonner


People also ask

How do I force my iPhone screen to rotate?

On an iPhone with a Home button, swipe up from the bottom of the screen to access it. On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.

How do I manually rotate my screen?

When Auto-rotate screen is turned off, you can still rotate the screen manually. Tap the rotate icon that appears in the bottom-right corner. You can also turn Auto-rotate screen on and off by swiping down twice from the top of the screen and tapping the button in the Quick Settings menu.


2 Answers

Found it, this code does the trick in the viewdidload:

self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
self.view.bounds = CGRectMake(0.0, 0.0, 460.0, 320.0);

Still have 1 odd thing. No matter what I do to set the navigation bar to hidden, it does not rotate, and stays at the left side of the view.

[[self navigationController] setNavigationBarHidden:YES animated:NO];

Has anyone seed this behavior and solved?

like image 172
Rob Bonner Avatar answered Oct 13 '22 01:10

Rob Bonner


I'm positive that you cannot 'force' a rotation. The system decides when to change the orientation of the device; so the 'orientation' properties are essentially read-only. I looked into this same problem a long time ago when I wanted to make sure a particular view always displayed in one orientation.

Due to this, most apps allow all of their views and view controllers to work in any of the orientations the app supports. I've found that trying to restrict the behavior of some views and view controllers ultimately creates more hassle, and can cause issues when transitioning between views and view controllers.

The code you posted will work for your view. You are not actually changing the orientation at all; your view is just behaving like it has been rotated by drawing in a rotated fashion. I'm not sure if you can do the same thing to the navigation bar or not, but it's worth a shot. If you are able to control the view properties of the navigation bar (it is a UIView as well), applying the same pattern you are using for your custom view should work.

Most apps that want a view to only be in landscape ultimately force their entire app to be in landscape. For instance, Flight Control only supports one orientation. Thus, the drawing code is pretty simple; regardless of orientation, just draw the view and rotate it to the one orientation it supports (either landscape left or right).

Your app's design wouldn't be that easy... it sounds like you are not designing a full-screen app. You would have to worry about the navigation bar and status bar being properly drawn.

like image 33
CJ. Avatar answered Oct 13 '22 01:10

CJ.