Forum Xamarin.iOS

Force one view (storyboard) show only in Portrait mode

BageraBagera USMember ✭✭

How I can force one view (view controller) appear in Portrait mode only?

I used this in my view controller class but it doesn't help

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() { return UIInterfaceOrientationMask.Portrait; }

Best Answer

  • BageraBagera US ✭✭
    Accepted Answer

    I fixed it like this:

    I added this function to keep the view in portrait mode:

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() { return UIInterfaceOrientationMask.Portrait; }

    And I added this function to launch in portrait mode

    `public override void ViewWillLayoutSubviews()
    {
    base.ViewWillLayoutSubviews();

            //Force the view to launch in portrait mode only
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
    
    
        }`
    

    Hope this will help someone :)

Answers

  • BageraBagera USMember ✭✭
    Accepted Answer

    I fixed it like this:

    I added this function to keep the view in portrait mode:

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations() { return UIInterfaceOrientationMask.Portrait; }

    And I added this function to launch in portrait mode

    `public override void ViewWillLayoutSubviews()
    {
    base.ViewWillLayoutSubviews();

            //Force the view to launch in portrait mode only
            UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
            UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
    
    
        }`
    

    Hope this will help someone :)

Sign In or Register to comment.