我正在使用两种受支持的语言(即英语和阿拉伯语)开发Xamarin.iOS应用程序。我想使用自定义的后退按钮。该按钮位于导航栏中,但是它有一些我想摆脱的多余空间。

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

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


请参考以下结果截图。我要摆脱以红色标记的空间

ios - 自定义后退按钮具有多余的多余空间-LMLPHP
ios - 自定义后退按钮具有多余的多余空间-LMLPHP

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

ios - 自定义后退按钮具有多余的多余空间-LMLPHP

最佳答案

您可以根据需要创建自定义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等)

关于ios - 自定义后退按钮具有多余的多余空间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57657484/

10-14 01:53