ios - 重新加载导航栏

标签 ios navigation uinavigationbar uinavigationitem

如何重新加载/重新分配导航栏项目?我使用一些更改导航栏的库,有时我有一个包,其中所有导航项都消失了。我在 viewWillAppear 中重新分配了正确的项目,例如:

UIButton *actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
actionButton.frame = CGRectMake(270, 0, 50, 50);
actionButton.adjustsImageWhenHighlighted = YES;
[actionButton setImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
[actionButton setImage:[UIImage imageNamed:@"share-active.png"] forState:UIControlStateHighlighted];
[actionButton addTarget:self action:@selector(presentActivity) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *actionBarButton = [[UIBarButtonItem alloc]initWithCustomView:actionButton];
actionBarButton.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = actionBarButton;

但它不起作用,有时我没有任何导航项。

最佳答案

UIBarButtonItem

不是 UIButton 的子元素。

这里没有像 setImage:forState: 等已知方法

像这样创建和定制一个 UITabBarButton

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
    newBackButton.image = [UIImage imageNamed:@"back"];
    self.navigationItem.leftBarButtonItem=newBackButton;

还要验证在 viewDidLoad 时是否会调用此代码的方法

关于ios - 重新加载导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23877079/

相关文章:

ios - 无法与 iOS 11 SearchController 中的 View 交互?

html - 下推内容的透明导航菜单

java - 未从 fragment 调用 onActivityResult

ios - UINavigationbar 会在 UISearchbar 结束搜索后显示,如何隐藏导航栏

ios - 访问所有具有相同 categoryBitMask 的单个对象

ios - Xcode Storyboard 严重问题变为空白?

ios - 在 Xcode UI 测试期间检查电子邮件?

ios - UINavigationBar - prefersLargeTitles - 如何在标题左侧设置图像?

swift - 右栏按钮图像太大 - Swift

iphone - 带有隐藏菜单的按钮 iPhone