Skip to content

kraigspear/TransparentModal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TransparentModal

How to do a transparent modal in Xamarin.iOS

Alt Text

Steps

  1. When presenting a UIViewController the ModalPresentationStyle is set to UIModalPresentationStyle.OverCurrentContext
 partial void ShowDetailAction(UIButton sender)
 {
    var detailViewController = Storyboard.InstantiateViewController("DetailView");
   //This is what makes presenting over another VC possible. 
   detailViewController.ModalPresentationStyle = UIModalPresentationStyle.OverCurrentContext;
   PresentViewController(detailViewController, true, null);
 }
 

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle

  1. Set the background of the modal view to be semi transparent / Set the View Opaque property to false
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    View.BackgroundColor = UIColor.Black.ColorWithAlpha(0.5f);
    View.Opaque = false;
}
  1. Include another non-transparent view, or embedded a container UIViewController to hold the focused content https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html

Keep in mind that adds a little complexity and may or may not be needed. It is not shown in this example.

  1. Add a tap gesture recognizer to close the view.
  • Go to the toolbox select Tap Gesture
  • Drag it to the view
  • Add an event handler
  • Close the ModalViewController using the DismissViewController method
partial void TapAction(UITapGestureRecognizer sender)
{
    DismissViewController(true, null);
}

About

How to do a transparent modal in Xamarin.iOS

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages