1
votes

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

4
please post your code tooFiloo
check orientation of interface in interface builderVolodymyr B.
how did you define the –shouldAutorotate method for iOS6+, and how did you define the –shouldAutorotateToInterfaceOrientation: for <iOS6 in your view controller?holex

4 Answers

4
votes

Select your target project and make sure you have choose following landscape mode:

enter image description here

1
votes

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];
    
0
votes

There are two mechanisms that control this.

You already know about the target properties. If this is all you change, this should take care of it.

There is also a lower level control, which you might have picked up from old template code. For each view controller, if you implement the method

shouldRotateToInterfaceOrientation:

That will override the setting for the target. I suspect the superclasses implementation just refers to the target setting.

0
votes

You can add UISupportedInterfaceOrientations property in YourProject-info.plist as follow, then it will only support landscape orientation.

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeRight</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
</array>

but do not know what's the meaning with your two pictures