ios - 从导航栏左右栏按钮项减少左侧空间和右侧空间

标签 ios xcode navigationbar

我在导航栏上添加了两个自定义按钮。 一个在左边,另一个在右边。 我已经成功完成了,

但是我没有减少按钮起点和框架之间的空间。

我试了很多,也给了x的负值,还是没用。 这是按钮的代码

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

也尝试过使用

aBarButtonItem.width = -16;

但没有帮助。

如何实现这个...? 提前致谢...

这些是我喜欢但没有太大帮助的一些链接 How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]

How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7]

enter image description here

最佳答案

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

像那样使用。

关于ios - 从导航栏左右栏按钮项减少左侧空间和右侧空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27244204/

相关文章:

ios - 显示自定义多个引脚会显示位置的错误引脚

html - 如何居中 "position: relative"- 导航栏?

ios - AVPlayer 是否支持直播事件倒带?

objective-c - 为 REST API 开发 iOS SDK

objective-c - Xcode 12,如何在整个项目中抑制 "Double-quoted include in framework header"警告

ios - 当我尝试导入或导出本地化时,为什么 Xcode 显示 "Duplicate localized resource info.plist found"?

Swift3:为什么这个简单的代码块需要很长时间才能编译

ios - 移动导航栏框架是否正确?

android - 从 onPostExecute(String result) 启动 Activity 不会设置 ui 标志

iPhone:以编程方式访问语音备忘录