【问题标题】:Remove space before left button on Navigation bar in IOS7?IOS7导航栏左键前删除空格?
【发布时间】:2013-11-13 10:34:17
【问题描述】:

我可以实现导航栏和添加左栏按钮,但是在左按钮有空间之前,请询问如何在ios 7中删除这个空间。

【问题讨论】:

标签: iphone button uinavigationbar


【解决方案1】:

使用它会起作用我已经使用过它并且在 iOS 7 上也可以正常工作

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】:

    你可以使用系统.fixedSpaceUIBarButtonItem

    这是一个产生预期结果的快速版本(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]
    

    另一个想法是直接将UIButton 添加到UINavigationController.view

    【讨论】:

    • 为什么negativeSpacer.width必须是-20?
    • 因为,正如评论中所写,这是iOS 10默认按钮空间左侧的空间量,因此您必须使用负宽度固定空间删除20px。
    【解决方案3】:

    我们可以添加自定义导航栏的左栏按钮,但在左按钮之前有空格。我解决了这个问题如下,

    来电:

    //  MyViewController.m
    
    - (void)viewDidLoad {
            UIBarButtonItem *bbBack = [Utilities overrideBackBarButtonItemWithTarget:self action:@selector(btnBackTapped:)];
    }
    
    - (void)btnBackTapped:(id)sender {
        //Custom navigation back bar button item
    }
    

    将此类方法添加到您的 Utilities 类或项目的任何公共类中,

    实施:

    //  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.png

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-03
      相关资源
      最近更新 更多