gpt4 book ai didi

ios - 将图像和标题添加到导航 backBarButtonItem

转载 作者:行者123 更新时间:2023-11-29 04:01:12 24 4
gpt4 key购买 nike

我正在尝试向导航 Controller 的后退按钮添加一个图标(并保留标题)。看来如果我设置 UIBarButtonItem 的图像它会隐藏标题,所以我想我应该尝试自定义 View 。我已经尝试过

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
[customButton setTitle:@"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;

但是 Apple 文档说 backBarButtonItem 会忽略自定义 View ,因此这不起作用。

我也试过这个:

self.navigationItem.leftBarButtonItem = backButton;
self.navigationItem.hidesBackButton = YES;

但是 leftBarButtonItem 太快显示一屏,并且 hidesBackButton 似乎没有隐藏 backBarButtonItem。

是否有另一种方法可以将图像和标题添加到导航 backBarButtonItem 上?

最佳答案

首先通过使用代码 self.navigationItem.hidesBackButton = YES; 隐藏由 UINavigationController 提供的后退按钮

并使用以下代码设置带有图像和标题的自己的按钮

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
[customButton setTitle:@"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;

关于ios - 将图像和标题添加到导航 backBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15836418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com