Remove space before left button on Navigation bar in IOS7?

最后都变了- 提交于 2020-01-02 09:53:48

问题


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.


回答1:


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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/19951433/remove-space-before-left-button-on-navigation-bar-in-ios7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!