Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 未知原因导致巴布通体变大_Objective C_Ios_Uibarbuttonitem - Fatal编程技术网

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进行“智能”调整大小。我不希望进行任何调整。但由于某些原因,它的行为就像它的插入将是活动的,它被调整大小以具有更大的宽度,大致上图片的中半部分被拉伸以提供更宽的按钮


为什么会这样?如何防止这种情况发生?

实现带有自定义图像的条形按钮项目的最简单方法是:

  • 将UIButton拖到情节提要上
  • 将按钮类型设置为“自定义”,并为该按钮设置图像
  • 将按钮拖到工具栏上
  • 情节提要会自动创建UIBarButtonItem,并将UIButton添加为子视图。或者,如果您不想使用故事板,可以实现以下功能:

    // 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,那么最好将它们显式地设置为零。