Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

iphone - How to restrict my app to landscape mode?

I have my iPad application created using the SplitView template. I wonder what is the best way to restrict my application to landscape mode?

I have tried overriding shouldAutorotateToInterfaceOrientation: method in DetailViewController.m

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

but 4.2 GM is still buggy and it fails to show the controller view. What other choices do I have?

Thanks in advance.

UPDATE1

  • I have already filed a bug report: Bug ID #8620135

  • My app is almost finished and I have to find a work-arround since I don't think they are going to solve this before 4.2 officially comes out (GM is already out!)

    In order to recreate the bug, just use SplitView template and override above method in any of the UIViewControllers (RootViewController or DetailViewControllers)

UPDATE2

I have found a work-around. (See UPDATE3 for the complete work-around)

Set UISupportedInterfaceOrientations only to support Landscape , this will force the app to start in landscape mode allowing DetailViewController to start correctly(hence shown correctly)

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

But if you rotate the device, it turns Portrait mode!!!, so is still necessary to override shouldAutorotateToIntercafeOrientation: as above

Discussion:

If this wouldn't be a bug I would expect a warning or execution error, exception or something when starting the app in a orientation that is not supported by the view controller. Besides, why only DetailViewController does not show? If this would be specification, then RootViewController should also fail to load then. Don't you think? thanks for you help... ;)

UPDATE3

After further tests I have realized that above work-around does not work in some cases. For example when starting the app when the device is in landscape won't work!. The real problem seems to be that in iOS4.2GM UISplitViewController needs all its controllers have all rotations to be available at its load time. So is necessary to trick him so it loads in Landscape mode and then not allow him to rotate its view controllers.

So here is the new work-around for this annoying iBug.

Step1: Set Info.plist like so:

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

Step2 Set a new flag in DetailViewController.m or .h (from SplitView Template)

BOOL lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //WORK-ARROUND: Bug ID# 8620135.
    if (lockRotation) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }else{
        return YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //set NO here since this is called before shouldAutorotateToInterfaceOrientation method is called
    lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
}
- (void)viewDidAppear:(BOOL)animated {
    //set YES as soon as possible, but after shouldAutorotateToInterfaceOrientation method was called
    lockRotation = YES; //WORK-ARROUND: Bug ID# 8620135.
    [super viewDidAppear:animated];
}

IMPORTANT NOTE: Please note that this bug only appears when the UISplitViewController is loaded and not everytime the its view appears. Hence, to see this bug make sure the app was terminated before.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I asked a question with a bounty of 500 that seems to be the same thing you're facing.

From my limited experience it is much easier to make a landscape-only iPhone app than a landscape-only iPad app. I'm not sure why there is any difference, but the steps Apple says to take to make it landscape-only do not work on their own.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...