Dar*_*ren 20 uinavigationbar uibarbuttonitem uinavigationitem ios
我正在创建一个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;
}
Run Code Online (Sandbox Code Playgroud)
它在我运行时看起来是正确的,但我可以通过点击导航栏从x = 0到大约100来选择条形按钮项目.如何调整可选区域的宽度为30像素?
rox*_*neM 20
从iOS 11开始,只为设置框架customView而UIBarButtonItem不是不起作用(如接受答案中建议的那样).似乎添加Autolayout来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])
Run Code Online (Sandbox Code Playgroud)
另请注意,您最好使用UIButton它CustomView作为您必须依赖它来触发操作.
Aar*_*ron 13
您可能考虑的一种方法是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;
Run Code Online (Sandbox Code Playgroud)
现在,这是我知道限制UIBarButtonItem添加到UINavigationControllers 的s 的自动调整大小的唯一方法navigationItem.
关键是要意识到您正在更改自定义视图的宽度,而不是UIBarButton自身.
因此代码是:
CGRect resizedFrame = myBarButtonItem.customView.frame;
resizedFrame.size.width = myNewWidth;
myBarButtonItem.customView.frame = resizedFrame;
Run Code Online (Sandbox Code Playgroud)
您还需要触发布局更改:
[myNavigationBar setNeedsLayout];
Run Code Online (Sandbox Code Playgroud)
所有这一切都不言而喻,布局是通过自动调整大小和帧来完成的.使用自动布局进入导航栏的过程没有成功.请参阅我的问题使用UINavigationBar和UIBarButtonItem进行自动布局.
抱歉,我意识到我的代码几乎与@oscartzombie相同.不是故意的!我将留下这个答案,因为我认为值得添加布局和其他点,除了解释没有参考Interface Bulder或图像专门.
这些解决方案都不适合我,我发现自动调整大小会覆盖您在代码中编写的尺寸。
通过 UIButton 方式创建按钮后,编写以下代码块:
[self.navigationItem.rightBarButtonItem.customView.widthAnchor constraintEqualToConstant:mWidth].active = YES;
[self.navigationItem.rightBarButtonItem.customView.heightAnchor constraintEqualToConstant:mHeight].active = YES;
Run Code Online (Sandbox Code Playgroud)
您可以通过从接口构建器将图像拖放到条形按钮项并使用以下代码更改自定义视图的宽度来执行此操作:
CGRect frame = self.navigationItem.leftBarButtonItem.customView.frame;
frame.size.width = 141;
self.navigationItem.leftBarButtonItem.customView.frame = frame;
Run Code Online (Sandbox Code Playgroud)
以下方法有效,请参阅更改按钮宽度和高度的代码以及添加操作项.
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;
Run Code Online (Sandbox Code Playgroud)
注意:UIBarButtonItem的目标和操作不适用于图像视图
| 归档时间: |
|
| 查看次数: |
54037 次 |
| 最近记录: |