iOS 7 - Display a View as Portrait Only for application built in Xamarin

瘦欲@ 提交于 2019-12-11 02:44:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!