我的 TabBarView 中有一个导航栏。我想要一个 rightBarButtonItem 和我的导航栏上的标题。为什么会这样?我的代码在 viewDidLoad 方法中。为什么我的标题显示为左键项?然后当我点击它时,它就像在移动一个 View ,然后我的右栏按钮项目消失了,标题是正确的。
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];
最佳答案
UINavigationItem 包含标题和栏按钮项。您正在创建多个导航项,而您应该只分配单个 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。
关于ios - 为什么我的标题显示为左键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16406705/