Search code examples
iosxamarin.iosuinavigationitem

Custom back button has extra unwanted space


I'm developing a Xamarin.iOS application with two supported languages i.e. English and Arabic. I want to use a custom back button. The button is placed in the navigation bar, however, it has some extra space which I want to get rid of.

Below is the code that I have used to add the button

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

Refer to the below screenshots of the result. I want to get rid of the space marked in red

English Arabic

Below is the image that I'm using as the back icon

enter image description here


Solution

  • You can create a custom navigationBar as you want .

    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;
            }
    
        }
    

    ou can set the property of title , backButton and navigationBar as you need (such as text , color ,BackgroundColor ,font e.g.)