尝试在左侧添加2个按钮,但是间距有些问题。有解决方案吗我试过负间距,但它不起作用。只有2个普通按钮启动:
self.navigationItem.leftBarButtonItems = @[self.menuButton, self.scoreLabel];
感谢您的帮助
答案 0 :(得分:3)
创建一个UIBarButtonItem,例如
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
将spacer的宽度设置为
spacer.width = -15.0f;
在self.menuButton和self.scoreLabel对象之间添加此间隔符,如下所示,
self.navigationItem.leftBarButtonItems = @[self.menuButton ,spacer , self.scoreLabel];
答案 1 :(得分:1)
通过为其指定背景颜色来检查您的乐谱按钮宽度,或者按钮标题可能是右对齐的。否则负间距效果很好。
//Create a bar button with negative spacing
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -15.0f;
//Bar Button 1
UIImage *sideImage = [UIImage imageNamed:@"icon1"];
UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu setImage:sideImage forState:UIControlStateNormal];
[btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];
[btnSideMenu setFrame:CGRectMake(0, 0, sideImage.size.width, sideImage.size.height)];
[btnSideMenu addTarget:self action:@selector(btnLeftMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];
//Bar Button 2
UIImage *sideImage1 = [UIImage imageNamed:@"icon2"];
UIButton *btnSideMenu1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu1 setImage:sideImage1 forState:UIControlStateNormal];
[btnSideMenu1 setImage:sideImage1 forState:UIControlStateHighlighted];
[btnSideMenu1 setFrame:CGRectMake(0, 0, sideImage1.size.width, sideImage1.size.height)];
[btnSideMenu1 addTarget:self action:@selector(btnLeftMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu1];
//Add all the bar buttons in the leftBarButtonItems Array
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:sideMenuItem1, negativeSpacer, sideMenuItem, nil];