Ios 减少导航栏左侧和右侧栏按钮项的左侧和右侧空间
我在导航栏上添加了两个自定义按钮。 一个在左边,另一个在右边。 我成功地完成了它 但是我没有缩小按钮起点和框架之间的空间 我试了很多,也给出了x的负值,仍然没有帮助。 这是按钮的代码Ios 减少导航栏左侧和右侧栏按钮项的左侧和右侧空间,ios,xcode,navigationbar,Ios,Xcode,Navigationbar,我在导航栏上添加了两个自定义按钮。 一个在左边,另一个在右边。 我成功地完成了它 但是我没有缩小按钮起点和框架之间的空间 我试了很多,也给出了x的负值,仍然没有帮助。 这是按钮的代码 -(void)addLeftButton { UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; aButton.backgroundColor = [UIColor redColor]; [aButton se
-(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;
但是没有帮助
如何做到这一点。。。?
先谢谢你
这些是我喜欢的一些链接,但没有太大帮助
像那样使用。这里有一个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)
你好,优素福,谢谢它的工作原理,之前我在setLeftBarButtonItems上直接添加了UIButton。现在它可以正常工作了,感谢iphone 6和6 plus,您必须使用不同的空间。@Yusufterzi,对我来说,我想在左栏按钮(后退按钮)上添加空间。我怎样才能做到这一点?我需要swift 4版本
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)