Objective c 未知原因导致巴布通体变大
我有一个这样初始化的按钮>Objective c 未知原因导致巴布通体变大,objective-c,ios,uibarbuttonitem,Objective C,Ios,Uibarbuttonitem,我有一个这样初始化的按钮> self.button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@s
self.button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doAction)];
[self.button setBackgroundImage:[UIImage imageNamed:@"customNavBar_button_right_enabled"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
稍后,我将为其附加自定义背景,如下>
self.button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doAction)];
[self.button setBackgroundImage:[UIImage imageNamed:@"customNavBar_button_right_enabled"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
然后把它放到导航栏上
[self.navigationItem setRightBarButtonItem:self.button
animated:NO];
此图像具有固定大小。我希望该按钮具有完全自定义的背景图像,没有使用UIEdgeInsets进行“智能”调整大小。我不希望进行任何调整。但由于某些原因,它的行为就像它的插入将是活动的,它被调整大小以具有更大的宽度,大致上图片的中半部分被拉伸以提供更宽的按钮
为什么会这样?如何防止这种情况发生?实现带有自定义图像的条形按钮项目的最简单方法是:
// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:@"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
// Initialize the UIBarButtonItem
UIBarButtonItem aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
// Set the Target and Action for aButton
[aButton addTarget:self action:@selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
希望有帮助
编辑:别忘了将AbarButtonim添加到UIToolbar。我采用了编程方法,如果您需要UI布局管理的灵活性,故事板太僵化了。问题是解决方案解决了问题,但在其他地方产生了更多问题,至少目前是这样。我在其他地方的代码依赖于这样一个事实,即按钮是UIBarButtonSystemItem,而不是UIButton。但也许我会克服它。我想如果我不能去掉内置的cap inset,那么最好将它们显式地设置为零。