ios - 为什么我的标题显示为左键?

标签 ios xcode uinavigationbar title

我的 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/

相关文章:

ios - 声明一个多类型数组

iPhone模拟器失败

iphone - 隐藏导航栏的标题

iOS汇编代码

ios - 如何监听信标并获得最近的信标

ios - 如何在iOS崩溃报告中找到苹果团队测试过的硬件型号

swift - 使用 Swift 3.0.1 编译的模块无法在 BuddyBuild 上的 Swift 3.0 中导入

objective-c - UINavBar 似乎在其他按钮上有一个不可见的层

ios - 在 iOS7 半透明导航栏中获取正确的颜色

iphone - 限制 UITableView 的滚动