Xamarin iOS 添加自定义后退按钮

Xamarin iOS adding Custom back button

我想自定义后退按钮,这样我就可以用确认框提示用户。
为此,我通过将其隐藏 属性 设置为 'True'
来删除默认导航后退按钮 我正在添加新的左栏按钮项目。

但是左栏按钮的 UI 与默认后退按钮不同。 请查看随附的屏幕截图。
请在下面找到添加左栏按钮的代码-

UIImage image = UIImage.FromBundle("BackButton");
UIButton customButton = new UIButton(UIButtonType.Custom);

customButton.SetImage(image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIControlState.Normal);
customButton.Frame = new CGRect(0, 0,
100, 44);
customButton.ImageEdgeInsets = new UIEdgeInsets(0,
-40, 0, -40);
this.NavigationItem.SetHidesBackButton(true, false);
var backButton = new
UIBarButtonItem(customButton);

this.NavigationItem.LeftBarButtonItem = backButton;


提前致谢!

您最好在设置背景图片之前缩放图片。


UIImage image = UIImage.FromBundle("BackButton").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);


UIImage newImg = ScalingImageToSize(image, new CGSize(30, 30)).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
UIButton customButton = new UIButton(UIButtonType.Custom);

            customButton.SetImage(newImg.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIControlState.Normal);
customButton.Frame = new CGRect(0, 0, 50, 50);
customButton.ImageEdgeInsets = new UIEdgeInsets(0, -40, 0, -40);

NavigationController.NavigationBar.BackIndicatorImage = newImg;
NavigationController.NavigationBar.BackIndicatorTransitionMaskImage = newImg;

UIBarButtonItem buttonItem = new UIBarButtonItem(customButton);

NavigationItem.BackBarButtonItem = buttonItem;
 public UIImage ScalingImageToSize(UIImage sourceImage, CGSize newSize)
    {

        if (UIScreen.MainScreen.Scale == 2.0) //@2x iPhone 6 7 8 
        {
            UIGraphics.BeginImageContextWithOptions(newSize, false, 2.0f);
        }


        else if (UIScreen.MainScreen.Scale == 3.0) //@3x iPhone 6p 7p 8p...
        {
            UIGraphics.BeginImageContextWithOptions(newSize, false, 3.0f);
        }

        else
        {
            UIGraphics.BeginImageContext(newSize);
        }

        sourceImage.Draw(new CGRect(0, 0, newSize.Width, newSize.Height));

        UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();

        UIGraphics.EndImageContext();

        return newImage;

    }

选项 2

您可以根据需要创建自定义导航栏。

public class xxxViewController: UIViewController
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);



            NavigationController.NavigationBar.Hidden = true;


            double height = IsiphoneX();

            UIView backView = new UIView()
            {
                BackgroundColor = UIColor.White,
                Frame = new CGRect(0,20,UIScreen.MainScreen.Bounds.Width, height),

            };

            // set 
            UIButton backBtn = new UIButton() {

                Frame = new CGRect(20, height-44, 40, 44),
                Font = UIFont.SystemFontOfSize(18),

            } ;


            UIImage image = UIImage.FromBundle("BackButton");

            UIImage newImg = ScalingImageToSize(image, new CGSize(30, 30));




 backBtn.SetBackgroundImage(newImg.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), UIControlState.Normal);



            backBtn.AddTarget(this,new Selector("GoBack"),UIControlEvent.TouchUpInside);

            UILabel titleLabel = new UILabel() {
                Frame=new CGRect(UIScreen.MainScreen.Bounds.Width/2-75, 0,150, height),
                Font = UIFont.SystemFontOfSize(20),
                Text = "xxx",
                TextColor = UIColor.Black,
                Lines = 0,

            };

            UILabel line = new UILabel() {

                Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
                BackgroundColor = UIColor.Black,

            };

            backView.AddSubview(backBtn);
            backView.AddSubview(titleLabel);
            backView.AddSubview(line);

            View.AddSubview(backView);
        }


         double IsiphoneX()
        {

            double height = 44;

            if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                if (UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
                {
                    height = 64;
                }
            }

            return height;
        }

        [Export("GoBack")]
        void GoBack()
        {
            NavigationController.PopViewController(true);
        }

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

            NavigationController.NavigationBar.Hidden = false;
        }

    }

您可以根据需要设置 属性 title , backButton 和 navigationBar (例如 text , color ,BackgroundColor ,font )