我有一个问题,到目前为止我找不到解决方案.我正在为我的应用添加一个新功能,并希望在我的UINavigationBar的左侧添加第二个UIBarButtonItem.出于某种原因,iOS 7将其作为button1,grandCanyon,button2.我找不到任何方法来消除这两个按钮之间的荒谬间距,这也导致我的标题不对齐.任何人都可以帮忙!?这个问题有方法解决吗!?
码:
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"firstButton"] style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)]; UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"secondButton"] style:UIBarButtonItemStylePlain target:self action:@selector(showAttachments)]; [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:firstButton,secondButton,nil]];
解决方法
想想我已经设法使用如下所示的自定义视图来解决问题,它并不完美(例如选择使按钮变暗而不是更亮)但我明天会尝试修复它.很高兴我的头痛结束了!谢谢你的帮助,它引导我采取了一些我没试过的新方法.
UIImage *firstButtonImage = [UIImage imageNamed:@"firstButton"]; firstButtonImage = [firstButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; UIButton *firstButton = [[UIButton alloc] initWithFrame:CGRectMake(0,35,35)]; [firstButton setImage:firstButtonImage forState:UIControlStateNormal]; [firstButton addTarget:self action:@selector(firstButtonPressed) forControlEvents:UIControlEventTouchUpInside]; UIImage *secondButtonImage = [UIImage imageNamed:@"secondButton"]; secondButtonImage = [secondButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(45,35)]; [secondButton setImage:secondButtonImage forState:UIControlStateNormal]; [secondButton addTarget:self action:@selector(secondButtonPressed) forControlEvents:UIControlEventTouchUpInside]; UIView *leftBarItemsView = [[UIView alloc] initWithFrame:CGRectMake(0,80,35)]; [leftBarItemsView addSubview:firstButton]; [leftBarItemsView addSubview:secondButton]; UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarItemsView]; [self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObject:leftBarItem]];