Remove space before left button on Navigation bar

2019-05-25 02:28发布

I can implement navigation bar and add left bar button, but before left button have space, please ask how to remove this space in ios 7.

enter image description here

3条回答
霸刀☆藐视天下
2楼-- · 2019-05-25 02:44

You could use a system .fixedSpace UIBarButtonItem.

Here is a swift version producing the expected result (iOS 10+):

let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -20 // working on iOS 10.
let barButton = UIBarButtonItem(title: "<# title #>", style: .plain, target: <# target #> , action: #selector(<# action #>))
navigationItem.leftBarButtonItems = [negativeSpacer, barButton]

Another idea is to add a UIButton to the UINavigationController.view directly.

查看更多
Anthone
3楼-- · 2019-05-25 02:54

We can add customize left bar button of navigation bar, but before left button have space. I fix this issue as following,

Calling:

//  MyViewController.m

- (void)viewDidLoad {
        UIBarButtonItem *bbBack = [Utilities overrideBackBarButtonItemWithTarget:self action:@selector(btnBackTapped:)];
}

- (void)btnBackTapped:(id)sender {
    //Custom navigation back bar button item
}

Add this class method to your Utilities class or any common class for your project,

Implementation:

//  Utilities.h

@interface Utilities : NSObject

#pragma mark - back bar button item
+(UIBarButtonItem*)overrideBackBarButtonItemWithTarget:(nullable id)target action:(nullable SEL)action;

@end


//  Utilities.m

@implementation Utilities

+(UIBarButtonItem*)overrideBackBarButtonItemWithTarget:(nullable id)target action:(nullable SEL)action  {

    // back button
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 65 , 44)];
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);//move image to the right
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barbuttonBack = [[UIBarButtonItem alloc] initWithCustomView:btn];

        //navigation spacer
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spacer.width = -15;// it was -6 in iOS 6  you can set this as per your preference

        //set leftBarButtonItems
    UIViewController *_self = (UIViewController*)target;
    [_self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:spacer,barbuttonBack, nil] animated:NO];

    return barbuttonBack;
}

@end

Back image: back.png

查看更多
成全新的幸福
4楼-- · 2019-05-25 02:55

use this it will work i have used it and works fine on iOS 7 also

UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithCustomView:segmentView];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                   target:nil action:nil];
   negativeSpacer.width = -6;// it was -6 in iOS 6  you can set this as per your preference
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,homeButton, nil] animated:NO];
查看更多
登录 后发表回答