I want to change the orientation of the single page. My whole application should be run in portrait but I want to make one page as Landscape/portrait (both).
I have taken reference from below links
http://www.appliedcodelog.com/2017/05/force-landscape-or-portrait-for-single.html
Code is working fine in Android but not in IOS.
AppDelegate.cs
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
var mainPage = Xamarin.Forms.Application.Current.MainPage;
if (mainPage is CartoonDetail_demo2 ||
(mainPage is NavigationPage && ((NavigationPage)mainPage).CurrentPage is CartoonDetail_demo2) ||
(mainPage.Navigation != null && mainPage.Navigation.NavigationStack.LastOrDefault() is CartoonDetail_demo2))
{
return UIInterfaceOrientationMask.All;
}
return UIInterfaceOrientationMask.Portrait;
}
But this condition is never going to be true in any case. Eventually I can not get the page from navigation stack.
So, I use easy way to do the same thing,
I want to make "CartoonDetail_demo2" this page as Landscape/Portrait.
In OnAppearing() method of the "CartoonDetail_demo2" page I have set one flag as true.
And OnDisappering() method of the "CartoonDetail_demo2" page I have set one flag as false.
use this flag in AppDelegate.cs.
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow) { if (AppConstants.IsOrientationAll) { return UIInterfaceOrientationMask.All; } return UIInterfaceOrientationMask.Portrait; }
Now, Condition become true whenever I go to the page "CartoonDetail_demo2".
But It is not working and my page is always in Portrait Orientation .
When I simply call this method without any condition then whole application is going in "All" (Landscape/Portrait) orientation.
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow) { return UIInterfaceOrientationMask.All; }
By default, My whole application in Portrait Orientation.
I have also tried few other demo but none of are going to working
should I use the plugin Plugin.DeviceOrientation?
in AppDelegate.cs
public bool allowRotation;
And rewrite the method
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, [Transient] UIWindow forWindow)
{
if(allowRotation==true)
{
return UIInterfaceOrientationMask.Landscape;
}
else
{
return UIInterfaceOrientationMask.Portrait;
}
}
Then you can call the following method in dependency service
in iOS
public class OrientationService : IOrientationService
{
public void Landscape()
{
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
}
public void Portrait()
{
AppDelegate appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;
appDelegate.allowRotation = true;
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}
in Android
public class OrientationService : IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void Portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
For more detail you can refer this similar issue .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.