自定义后退按钮有额外的不需要的 space

Custom back button has extra unwanted space

我正在开发一个 Xamarin.iOS 应用程序,它支持两种语言,即英语和阿拉伯语。我想使用自定义后退按钮。该按钮位于导航栏中,但是,它有一些额外的 space 我想去掉。

下面是我用来添加按钮的代码

var leftBarButton = new UIBarButtonItem(UIImage.FromBundle("IcArrowLeft.png"), UIBarButtonItemStyle.Plain, (s, e) =>
{
    DismissViewController(true, null);
});

参考下面的结果截图。我想去掉标记为红色的 space

下面是我用作后退图标的图片

您可以根据需要创建自定义 navigationBar

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),

            };


            UIButton backBtn = new UIButton() {

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

            } ;



            backBtn.SetBackgroundImage(UIImage.FromBundle("IcArrowLeft.png"),UIControlState.Normal);
            backBtn.ImageEdgeInsets = new UIEdgeInsets(0, -10, 0, 0);
            backBtn.TouchUpInside += BackButton_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;
        }

        private void BackButton_TouchUpInside(object sender, EventArgs e)
        {
            DismissViewController(true, null);
        }

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

            NavigationController.NavigationBar.Hidden = false;
        }

    }

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