我的TabBarView中有一个navigationBar。我想在我的navigationBar上有一个rightBarButtonItem和一个标题。为什么会这样?我的代码在viewDidLoad方法中。为什么我的标题显示为左键项目?然后,当我点击它时,它就像是在移动一个视图,然后我的右边的按钮项目消失了,标题是正确的。
UINavigationBar *myNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320,40)];
myNavBar.barStyle = UIBarStyleBlackOpaque;
[self.view addSubview:myNavBar];
//Creates the title.
UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"My Title"];
[myNavBar pushNavigationItem:title animated:NO];
//Creates the button.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self
action:@selector(showMail:)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];
navItem.rightBarButtonItem = button;
// add the UINavigationItem to the navigation bar
[myNavBar pushNavigationItem:navItem animated:NO];
答案 0 :(得分:4)
UINavigationItem包含标题和栏按钮项。您正在创建多个navigationItem,而您应该只分配单个UINavigationItem的属性。
看一下头文件,你会看到它包含你需要的一切:
NS_CLASS_AVAILABLE_IOS(2_0) @interface UINavigationItem : NSObject <NSCoding> {
@private
NSString *_title;
NSString *_backButtonTitle;
UIBarButtonItem *_backBarButtonItem;
NSString *_prompt;
NSInteger _tag;
id _context;
UINavigationBar *_navigationBar;
UIView *_defaultTitleView;
UIView *_titleView;
UIView *_backButtonView;
NSArray *_leftBarButtonItems;
NSArray *_rightBarButtonItems;
NSArray *_customLeftViews;
NSArray *_customRightViews;
BOOL _hidesBackButton;
BOOL _leftItemsSupplementBackButton;
UIImageView *_frozenTitleView;
}
只需指定一个实例即可。你不应该分配多个UINavigationItems。
答案 1 :(得分:0)
您正在创建多个导航按钮。您应该将所有这些属性设置为单个按钮。如果要为标题设置标题,可以将其设置为self.title = @“title”;
您也可以尝试设置navigationbar.titleview。