开发者

How do I initialize a view to an interface orientation?

开发者 https://www.devze.com 2023-01-11 22:25 出处:网络
The problem is that I am launching in-app email and triggering keyboards (via UITextFields) and they are coming up portrait on my landscape app.I am able to rotate it portrait, then rotate it landscap

The problem is that I am launching in-app email and triggering keyboards (via UITextFields) and they are coming up portrait on my landscape app. I am able to rotate it portrait, then rotate it landscape again, observing the black rotation corners animation that occurs when an app rotates from portrait to landscape. Since I've locked out any orientation but landscape right in my shouldAutorotateToInterfaceOrientation() method, the view itself doesn't rotate, which is great. However, when I add these views, the app keeps thinking they are portrait as is evidenced by the UITextField keyboard and in-app email orientation unless I rotate to portrait, then back to landscape (which triggers the autorotate method to return true.

Any suggestions on how I can make this app know it's already in landscape to begin with when I'm adding views?

Update: I had tried setting the status bar orientation, but it does not work for me. I don't know if there's something with the fact that I remove and then add views to the app that is confusing its understanding of the orientation. Do you know how apple objects such as the UITextField and in-app mail determine what orientation to show? Do they simply poll the sharedApplication orientation? The odd thing for me is that it seems like when I remove开发者_C百科 and add new views, these apple widgets that I've tilted the phone to change to landscape suddenly revert to showing as portrait. i.e. this general sequence of events:

1) app in landscape, trigger keyboard, it shows portrait

2) tilt device to make it go into landscape. close and open keyboard and it's in landscape.

3) remove and add some views.

4) trigger keyboard again, it shows up in portrait


[UIApplication setStatusBarOrientation:animated:] should give you what you want.


Setup Initial interface orientation field in Project plist


Interestingly, I found that setting the status bar orientation at the launch was not enough. Watching the field value, I noticed it was resetting to portrait at various times, such as when returning from canceling an in-app mail. I found making multiple calls to set the status bar seems to address the problem. Thanks for the input, mbehan.


Removing/adding views shouldn't change the status bar orientation; as far as I know, that defaults to the view controller's orientation unless you set it explicitly (and then you have to be careful, because view controllers reset it). You can try using UIStatusBarStyleBlackTranslucent and setting UIViewController.wantsFullScreenLayout on your VCs to see what's going on.

(In the past, I've noticed it getting confused when I add a view to the window directly, on top of a portrait-only view. It seems to work in all other cases though...)

What's your implementation(s) of shouldAutorotateToInterfaceOrientation:?


try this:

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

//Rotate the complete view to look like landscape view
    [self rotateToLandscape];
}

and the rotation method;

-(void)rotateToLandscape
{
    UIWindow *win = [[UIApplication sharedApplication]keyWindow];
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
    [[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor blackColor]];
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    win.transform = CGAffineTransformIdentity;
    win.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
    win.bounds = CGRectMake(0.0f, 0.0f, 480, 320);
    win.center = CGPointMake(160.0f, 240.0f);
    [UIView commitAnimations];
}   

}

0

精彩评论

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

关注公众号