How to update the navigation bar color in UIImagePickerController in Xamarin Forms (via WebView/JS)

Sagar S. Kadookkunnan 6 Reputation points
2020-11-24T02:10:17.577+00:00

In one of my Xamarin Forms project, I am in a situation where the webview triggers the opening of native UIImagePickerController (iOS) for Photo selection/Camera via JavaScript. In my app we use Transparent/Clear color for the Navigation bar mostly and only when the UIImagePickerController is presented, we want to change it to default color or some other Tint colour for the Navigation Bar (appearance).

Is there a way for us to do it easily? I did some research and looks like UIImagePickerController does not like to be wrapped up and so there is no Custom Renderers written in Xamarin Forms SDK for that.

How can I detect initialization of a UIImagePickerController natively and make the UINavigationBar appearance change for that change? Also, I want to detect the closing/photo picking from the UIImagePickerController and reverse the UINavigationBar appearance to the normal (clear color) one for our app as well.

Any suggestions would be greatly appreciated and Thank you in advance!

Sincerely,
Sagar S. Kadookkunnan

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2020-11-24T10:01:27.563+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You could try to detect if the UIImagePickerController present/dismiss in ViewWillDisappear/ViewWillAppear method of a ViewController, so you need to create a custom renderer of Page.

    Sample code

       [assembly : ExportRenderer(typeof(YourPage),typeof(MyRenderer))]  
       namespace CollectionViewFooterRefreshViewRepro.iOS  
       {  
           class MyRenderer : PageRenderer  
           {  
               public override void ViewWillDisappear(bool animated)  
               {  
                   base.ViewWillDisappear(animated);  
    
                   if(UIApplication.SharedApplication.KeyWindow.RootViewController is UIImagePickerController)  
                   {  
                       UINavigationBar.Appearance.BarTintColor = UIColor.Red;  
                   }  
               }  
    
    
               public override void ViewWillAppear(bool animated)  
               {  
                   base.ViewWillAppear(animated);  
    
                   if (UIApplication.SharedApplication.KeyWindow.RootViewController is UIImagePickerController)  
                   {  
                       UINavigationBar.Appearance.BarTintColor = UIColor.Clear;  
                   }  
               }  
           }  
       }  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.