开发者

Auto-rotation support for view added via presentModalViewController?

开发者 https://www.devze.com 2022-12-29 04:21 出处:网络
It seems that no matter what orientation I am supporting on my views in my app, when I display a view with presentModalViewController, the app snaps into portrait view.How can I support different orie

It seems that no matter what orientation I am supporting on my views in my app, when I display a view with presentModalViewController, the app snaps into portrait view. How can I support different orientations with 开发者_如何学编程a modal view controller?


The controller that you present using presentModalViewController:animated: should support any orientation, typically using this method:

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

With this method, when you present your controller in landscape orientation, it will correctly appear in landscape orientation, too.


I had same problem. I set TRUE AutorotateInterfaceOrientation but at the begin my modalview was always presented Portrait even if it was Landscape. Only if i changed device orientation (in emulator) it's behavior became correct. But at start no! I don't know why, but all depends from where you present view. If you present, for example, from viewDidLoad method, then problem occurs. For solving problem, i call presentModalView from a method that's called in viewDidLoad (apparently, it seems same thing). So, i did something like:

- (void) viewDidLoad
{
    [super viewDidLoad];
    // add this line
    [self performSelector:@selector(presentModal) withObject:nil afterDelay:0];
}

-(void) presentModal{
    // here present your view
}

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

This works for me


I just solved this problem for myself by adding code in viewDidAppear. I think the reason it didn't show up in the proper orientation for me is because I had the show modal code in viewDidLoad and at that point, I think the view had just loaded but the orientation was not yet set. So, adding it in viewDidAppear worked for me. You will probably also have luck in viewWillAppear (because by that time, it has to know what the orientation, bounds, sizes, etc are).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号