Home > Net >  Xamarin iOS: Custom back button image is too small
Xamarin iOS: Custom back button image is too small

Time:04-25

I have a custom UINavigationController and the following code in that controller to provide a custom back button image:

public override void PushViewController(UIViewController viewController, bool animated)
{
     base.PushViewController(viewController, animated);
        
     viewController.NavigationItem.BackButtonDisplayMode = UINavigationItemBackButtonDisplayMode.Minimal;
 }

public override void ViewWillAppear(bool animated)
{
   base.ViewWillAppear(animated);

   SetNeedsStatusBarAppearanceUpdate();
        
   var uiNavigationBarAppearance = new UINavigationBarAppearance();

   // ...
   // configure common UINavigationBarAppearance properties
   // ...

   var img = UIImage.FromBundle("back");
   uiNavigationBarAppearance.SetBackIndicatorImage(img, img);

   NavigationBar.StandardAppearance = uiNavigationBarAppearance;
}

This works fine, but I noticed the back image (which is a PDF asset) is too small compared to NavigationItem.RightBarButtonItems. In fact, if I use the same PDF as a RightBarButtonItem, it will be bigger.

Is it possible to configure the size of the back button image so it looks normal?

P.S. Minimum supported OS version is 15.4.

CodePudding user response:

You could use UIGraphicsImageRenderer to resize the original image , try the following code .

var img = UIImage.FromBundle("back");

var factor = 1.5;   //the value you can control
var newSize = new CoreGraphics.CGSize(factor*img.Size.Width,factor*img.Size.Height);

var renderer = new UIGraphicsImageRenderer(newSize);

var image = renderer.CreateImage((context) => {
    img.Draw(new CoreGraphics.CGRect(0,0,newSize.Width,newSize.Height));
});

uiNavigationBarAppearance.SetBackIndicatorImage(image, image);
  • Related