2014-12-02 109 views
1

我正在导航栏上添加两个自定义按钮。 一个在左边,另一个在右边。 我成功完成它,从导航栏左右栏缩小左侧空间和右侧空间栏按钮项

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

我试了很多,也给了x的负值,还是没有帮助。 下面是使用

aBarButtonItem.width = -16; 

的按钮

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

也尝试了代码,但没有帮助。

如何实现这个...? 在此先感谢...

这些都是一些链接,我更喜欢,但并没有太大帮助 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

回答

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

使用这样的。

+0

嗨Yusuf,感谢它的工作原理,我以前直接在setLeftBarButtonItems上添加UIButton。 现在它的工作正常 谢谢 – Sagar 2014-12-02 08:54:49

+0

对于iPhone 6和6加你必须使用不同的空间。 – 2014-12-02 09:43:12

+1

@Yusufterzi,对我来说,我想给左边的按钮(后退按钮)添加空格。我怎样才能做到这一点? – 2017-04-11 04:26:11

相关问题