问题
I am developing an application for iOS in Xamarin, I have a login screen as my first view which I want to display as portrait only and the rest can do landscape or whatever.
As this is in C# these are the methods I have overridden in my ViewController
but the view still rotates on my iPhone
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations ()
{
return UIInterfaceOrientationMask.Portrait;
}
public override bool ShouldAutorotate ()
{
return false;
}
I am probably missing something very simple but I just can't get it to work.
I am also attempting to use AutoLayout if that makes any difference.
回答1:
First make sure you have checked all the orientations in General settings:

and in whatever UIViewController
you wish only the portrait mode add the following snippet:
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
回答2:
@user1049099 I'm using Xamarin too with a UIViewController
, I managed to force portrait, but this only works on iPhone. On Ipad, if launching the app in landscape mode, it gets stuck.
[MonoTouch.ObjCRuntime.Since (7, 0)]
public override bool ShouldAutorotate ()
{
return false;
}
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations ()
{
return UIInterfaceOrientationMask.Portrait;
}
public override UIInterfaceOrientation PreferredInterfaceOrientationForPresentation ()
{
return UIInterfaceOrientation.Portrait;
}
When calling those three functions manually, they return false, Portrait and Portrait (in order) even if it's not the case on Ipad.
来源:https://stackoverflow.com/questions/21348964/ios-7-display-a-view-as-portrait-only-for-application-built-in-xamarin