'Xamarin Forms iOS RootViewController.View.AddSubview() not showing element in a page with Shell.PresentationMode="ModalAnimated"
I'm trying to add a floating Button for all my screens in a Xamarin forms app using Dependency service for each platform. iOS is working fine with any ContentPage but it fails when I set Shell.PresentationMode="ModalAnimated", button is not showing or is printing behind of ContentPage with animated modal.
Calling dependency service in my all views in Xamarin Forms
DependencyService.Get<IDeviceService>().ShowFloatingButton();
Dependency service implementation for iOS
namespace WebviewSample.iOS.DependencySeivices
{
public class DeviceService : IDeviceService
{
private void ShowFloatingButton()
{
var vc = UIApplication.SharedApplication.KeyWindow.RootViewController;
var btn = UIButton.FromType(UIButtonType.System);
btn.Frame = new CGRect(20, 200, 280, 44);
btn.SetTitle("Click Me", UIControlState.Normal);
vc.View.BackgroundColor = UIColor.DarkGray;
vc.View.AddSubview(btn);
vc.View.BringSubviewToFront(btn);
}
}
}
I have tried to get the top controller as below samples:
if (UIApplication.SharedApplication.KeyWindow.WindowLevel != UIWindowLevel.Normal)
{
foreach (var item in UIApplication.SharedApplication.Windows)
{
if (item.WindowLevel == UIWindowLevel.Normal)
{
vc = item.RootViewController;
break;
}
}
}
Also
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
Solution 1:[1]
According to this document:https://devblogs.microsoft.com/xamarin/xamarin-forms-shell-quick-tip-modal-navigation/
Xamarin shell navigation is different than traditional navigation where a page is pushed onto the stack so a new page pops up over the current page.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Adrain Zhu -MSFT |
