Xamarin iOS:自定义后退按钮图像太小
Xamarin iOS: Custom back button image is too small
我有一个自定义 UINavigationController
和该控制器中的以下代码以提供自定义后退按钮图像:
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;
}
效果很好,但我注意到与 NavigationItem.RightBarButtonItems
相比,背面图像(PDF 资产)太小了。事实上,如果我使用与 RightBarButtonItem
相同的 PDF,它会更大。
是否可以配置后退按钮图像的大小使其看起来正常?
P.S。支持的最低 OS 版本是 15.4。
您可以使用 UIGraphicsImageRenderer
调整原始图像的大小,请尝试以下代码。
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);
我有一个自定义 UINavigationController
和该控制器中的以下代码以提供自定义后退按钮图像:
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;
}
效果很好,但我注意到与 NavigationItem.RightBarButtonItems
相比,背面图像(PDF 资产)太小了。事实上,如果我使用与 RightBarButtonItem
相同的 PDF,它会更大。
是否可以配置后退按钮图像的大小使其看起来正常?
P.S。支持的最低 OS 版本是 15.4。
您可以使用 UIGraphicsImageRenderer
调整原始图像的大小,请尝试以下代码。
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);