-->

在一个UINavigationBar的一个的UIBarButtonItem的变化幅度(Change

2019-06-25 09:55发布

我创建的UIBarButtonItem并将其添加到我的导航栏,如下所示:

(void)viewDidLoad { 

   ...

   // Add the refresh button to the navigation bar
   UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [refreshButton setFrame:CGRectMake(0,0,30,30)];
   [refreshButton setImage:[UIImage imageNamed:@"G_refresh_icon.png"] forState:UIControlStateNormal];
   [refreshButton addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventTouchUpInside];
   UIBarButtonItem *refreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:refreshButton] autorelease];
   self.navigationItem.leftBarButtonItem = refreshBarButton;
}

它看起来是正确的,当我跑,但我可以通过点击导航栏中的任意位置从x = 0至约100.我怎样才能调整可选区域有30像素的宽度选择栏按钮项?

Answer 1:

你可以考虑的一种方法是创建一个UIBarButtonItem通过调用initWithCustomView: 这不是在理想的你做开箱没有得到“选择”的状态,你必须复合接壤的背景(如果想看起来)与您的按钮图像,但你可以更直接指定为框架的工具栏项目。 如果您使用的文字作为标题,而不是图片,你可能仍然需要在背景图像添加为子视图。 无论如何,我现在有同样的问题与此代码的工作对我来说:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];

self.navigationItem.leftBarButtonItem = barButtonItem;

现在,这是我所知道限制的自动调整大小的唯一途径UIBarButtonItem增补到UINavigationControllernavigationItem

或尝试Maggie的解决方案,这是比我更彻底 。



Answer 2:

由于iOS的11,只设置帧customView ,而不是UIBarButtonItem将无法正常工作(如建议在接受的答案 )。 好像加入自动布局到UIBarButtonItem覆盖设定框架。 这篇文章给我的想法:

navigationItem.leftBarButtonItem?.customView = yourCustomButtonView
let desiredWidth = 35.0
let desiredHeight = 35.0

let widthConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredWidth)
let heightConstraint = NSLayoutConstraint(item: yourCustomButtonView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: desiredHeight)

yourCustomButtonView.addConstraints([widthConstraint, heightConstraint])

还要注意,最好是你使用UIButton作为你的CustomView ,你必须依靠它来触发动作。



Answer 3:

您可以将图像从界面生成器拖放到栏按钮项并修改与此代码自定义视图的宽度做到这一点:

CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;


Answer 4:

这样做的关键是要意识到你正在改变自定义视图的宽度,而不是UIBarButton本身。

因此,该代码是:

CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;

您还需要触发布局的变化:

[myNavigationBar setNeedsLayout];

所有这一切都不用说,布局正与自动调整大小和框架完成。 入侵与自动布局导航栏取得没有成功。 见我的问题自动布局与UINavigationBar的和的UIBarButtonItem 。

抱歉刚刚意识到我的代码几乎是相同的@oscartzombie。 不是故意的! 因为我认为这是值得加入的布局和其他点,除了没有提及接口工具的详情或图像的具体解释,我会离开这个答案。



Answer 5:

下面的方法工作,看到代码更改宽度和按钮的高度,并添加行动项目也是如此。

UIButton *imageButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
[imageButton setBackgroundImage:[UIImage imageNamed:@"message_button"] forState:UIControlStateNormal];
[imageButton addTarget:self action:@selector(messageButtonTapped) forControlEvents:UIControlEventAllEvents];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageButton];
self.navigationItem.leftBarButtonItem = leftBarButtonItem;

注意:目标和的UIBarButtonItem的动作并不适用于图像视图



Answer 6:

这些解决方案中没有工作对我来说,我想出自动调整大小的东西覆盖您在代码中写道尺寸。

通过的UIButton方式创建按钮后,写入下面的代码块:

[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;


文章来源: Change width of a UIBarButtonItem in a UINavigationBar