Issue
I'm attempting to create a fullscreen overlay for my app. To do this, I need to override the renderer for NavigationPage, because no other item can be positioned over the navigation bar, even with absolute positioning.
I have a custom renderer where I'm attempting to draw over navigation bar like this:
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationPageRenderer))]
namespace MyApp.iOS.Controls
{
public class CustomNavigationPageRenderer : NavigationRenderer
{
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
using CGContext context = UIGraphics.GetCurrentContext();
...
}
}
}
However, context is always null. In examples like this and this they say you need to call UIGraphics.GetCurrentContext() from within the overridden Draw() function, but no such function exists within NavigationRenderer.
On Android it can be done by overriding NavigationPageRenderer.DispatchDraw(), but that does not exist on the iOS NavigationRenderer. How do I draw on this control in iOS?
Solution
We can use Dependency service and add a translucent background overlay on the root window .
Sample code
public interface IMyInterface
{
void present();
}
IMyInterface service = DependencyService.Get<IMyInterface>();
service.present();
[assembly: Dependency(typeof(IMyInterface))]
namespace ColeForms.iOS
{
public class MyInterface : IMyInterface
{
UIView overlay;
public MyInterface()
{
overlay = new UIView();
//full screen
overlay.Frame = UIScreen.MainScreen.Bounds;
//translucent background
overlay.BackgroundColor = UIColor.FromWhiteAlpha(0.5f,0.5f);
//add the tap gesture to dismiss overlap
overlay.AddGestureRecognizer(new UITapGestureRecognizer(()=> {
overlay.RemoveFromSuperview();
}));
}
public void present()
{
UIApplication.SharedApplication.KeyWindow.RootViewController.View.Add(overlay);
}
}
}
Screen shot
Answered By - ColeX - MSFT

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.