简体   繁体   中英

Monotouch - NavigationBar with extra padding ios 7


I'm trying to adapt my app for iOS 7. It's written with Xamarin and C#.
I'm having trouble with extra padding for the left button in the navigationbar.
I have a helper method for rendering my back-button which looks like this:

public static UIBarButtonItem GetBackButton (this UIViewController controller)
    {
        var backImage = new UIImage ("Images/back.png");
        var backButton = new UIButton (UIButtonType.Custom);
        backButton.Frame = new RectangleF (0, 0, 44, 44);
        backButton.SetImage (backImage, UIControlState.Normal);
        backButton.TouchUpInside += (object sender, EventArgs e) => {
            var cancelBackNavigation = false;
            if (controller is UIViewControllerBase) {
                if (((UIViewControllerBase)controller).PrepareNavigateBack () != true) {
                    cancelBackNavigation = true;
                }               
            }
            if (cancelBackNavigation == false) {
                controller.NavigationController.PopViewControllerAnimated (true);
            }
        };

        return new UIBarButtonItem (backButton);
    }


The navigationbar adds lots of padding before the back-button and making the image inside the back-button look very far away from its real position. The code above works fine in iOS 6.
I don't wanna use ContentEdgeInsets cause it will stretch the image and making it ugly.
Anyone with an idea of what to do?
在此处输入图片说明

I tried looking up your issue and found out that first, you need to hide the back button as follows:

NavigationItem.HidesBackButton = true;

Then for setting the button, you need to set it this way:

NavigationItem.BackBarButtonItem = yourButton;

That way, you won`t have the extra indentation.

Also you might find the following question useful: Make a custom back button for UINavigationController

This is how I setup my NavigationBar

controller.View.BackgroundColor = Theme.BackgroundColor;
controller.NavigationItem.SetHidesBackButton (true, false);
controller.NavigationController.Toolbar.TintColor = Theme.BackgroundColor;
controller.NavigationController.NavigationBar.TintColor = Theme.BackgroundColor;
controller.NavigationController.SetNavigationBarHidden (show == false, false);
controller.NavigationController.NavigationBar.BackgroundColor = Theme.BackgroundColor;
controller.NavigationController.NavigationBar.SetTitleTextAttributes (Theme.NavigationBarTextAttributes);
controller.NavigationController.NavigationBar.Subviews [0].Alpha = 0.01f;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM