Search code examples
iosipaduikituiinterfaceorientation

How to set iOS device orientation only landscape?


I'm writing a simple app under MacOS using Xcode. I want my app to be in landscape mode always. So I set landscape orientation in project properties. But when I add a button to my window.subview, this button doesn't appear in landscape position.

I have only AppDelegate class. I've changed function: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I've added:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor purpleColor];
[self.window makeKeyAndVisible];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setBackgroundColor:[UIColor whiteColor]];
[button setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2 - 50, [[UIScreen mainScreen] bounds].size.height/2,
                            100, 100)];
[button setTitle:@"Button" forState:UIControlStateNormal];
[self.window addSubview:button];

enter image description here

Update: I added [self.window setTransform:CGAffineTransformMakeRotation(-M_PI/2)];

and got: enter image description here


Solution

  • I've solved it like this:

    1. create my own RootViewController (like in How to create root view controller)

    2. then:

      self.rootController = [[AppRootViewController alloc] init];
      
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      self.window.rootViewController = self.rootController;
      [self.window addSubview:self.rootController.view];
      
      UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
      ....
      
      [[[self.window rootViewController] view] addSubview:button];