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;
}
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.
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.