简体   繁体   English

减少导航栏左右栏按钮项的左右空间

[英]reduce left space and right space from navigation bar left and right bar button item

I am adding two custom buttons on navigation bar. 我在导航栏上添加了两个自定义按钮。 one at left and other at right. 一个在左边,另一个在右边。 I am successfully done with it, 我已经成功完成了

but I failed to reduce the space between the starting point and frame of buttons. 但是我未能减小按钮的起点和框架之间的距离。

I tried a lot, also gave the negative value of x, still didn't helped. 我做了很多尝试,还给了x的负值,但还是没有帮助。 Here is the code for buttons 这是按钮的代码

-(void)addLeftButton
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.backgroundColor = [UIColor redColor];
    [aButton setTitle:@"H" forState:UIControlStateNormal];
    aButton.frame = CGRectMake(0.0, 0.0, 40, 40);
    [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    [aButton addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}

-(void)addRightButton
{
    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.backgroundColor = [UIColor greenColor];
    [aButton setTitle:@"B" forState:UIControlStateNormal];
    aButton.frame = CGRectMake(0.0, 0.0, 40, 40);
    [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
    [aButton addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:aBarButtonItem];
}

Also tried using 也尝试使用

aBarButtonItem.width = -16;

but didn't helped. 但没有帮助。

How to achieve this...? 如何实现这一目标? Thanks in advance... 提前致谢...

These are the some links that I preferred but didn't helped much How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7] 这些是我更喜欢但没有多大帮助的一些链接。 如何在UINavigationBar [iOS 7]中编辑左右UIBarButtonItem的空白空间

How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7] 如何在UINavigationBar [iOS 7]中编辑左右UIBarButtonItem的空白空间

在此处输入图片说明

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                           target:nil action:nil];
    negativeSpacer.width = -16;
    [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,yourbutton, nil] animated:YES];

Use like that. 这样使用。

Here's a Swift version: 这是Swift版本:

let negativeSpacer = UIBarButtonItem()
negativeSpacer.width = -16

let hamburgerButton = UIBarButtonItem(image: UIImage(named: "hamburgerMenu")?.withRenderingMode(.alwaysOriginal),
                                              style: .plain,
                                              target: self,
                                              action: #selector(menuButtonPressed))

navigationItem.setLeftBarButtonItems([negativeSpacer, hamburgerButton], animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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