Tuesday, October 21, 2008

Starting in Landscape Mode without Status Bar

Here's another situation where the Simulator doesn't behave the same as the device. It's also something that's not well documented, so let's take a second to look at how to have an application start up in landscape mode without a status bar.

The first thing you have to do is open up Info.plist in your Xcode project. It will be in the Resources group. You need to add two entires. First, add an entry called UIInterfaceOrientation and assign it a value of UIInterfaceOrientationLandscapeRight. Second, ad another entry, and call it UIStatusBarHidden. When you add this row, it should change to a checkbox; make sure the box is checked.

Now, in your application delegate, find the method called applicationDidFinishLaunching:. In it, you need to add the following code, after the last line that's already there. The code you need to add should look like this:


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

#if (TARGET_IPHONE_SIMULATOR)
UIScreen *screen = [UIScreen mainScreen];
viewController.view.bounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width);
viewController.view.transform = CGAffineTransformConcat(viewController.view.transform, CGAffineTransformMakeRotation((M_PI * 90 / 180.0)));
viewController.view.center = window.center;
#endif


After adding this code, your application, when run on the iPhone will rotate and resize the root view automatically just by virtue of having set the statusBarOrientation to UIInterfaceOrientationLandscapeRight. That's not the case when running it on the simulator, which is why the block of code in the conditional is necessary. With that, the code should behave exactly the same on the device and in the simulator.

To compile this code, you're also going to need to add the CoreGraphics framework. Make sure to add the framework using a reference type of "Relative to Current SDK'. The version for the device is located here:

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.1.sdk/System/Library/Frameworks/CoreGraphics.framework



5 comments:

bmantzey said...

The following code:

#if (TARGET_IPHONE_SIMULATOR)
UIScreen *screen = [UIScreen mainScreen];
viewController.view.bounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width);
viewController.view.transform = CGAffineTransformConcat(viewController.view.transform, CGAffineTransformMakeRotation((PI * 90 / 180.0)));
viewController.view.center = window.center;
#endif

Does not work because I do not have any viewController. Is there a way to accomplish this same thing without the view controller? If not, is this step still necessary? If so, how do I convert my current project to have a view controller?

I started with the GLGravity example, so the code that it started with was EAGLView.h/m and the AppDeligate code. Thanks.

Aegeon said...

thanks this helped me alot

also you can do this with the GLGravity example (this I started with a similar codebase that did not have a view controller)

you just take your appDelagate class and search for you EAGLView

in my case this was called glView

then replace all the viewController.view's with glViews

so glView.bounds = .....


hapy coding

Gary said...

Generic Viagra is deeply nice for me as this is too ever since I love every single thing that is somehow related to landscapes.I hope my life is time enough to see many different kind of landscapes all over the world

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels

miku said...

when adding this code view displays in landscape but it does not navigate to next view event is fired but view does not change