首页 文章

为什么我的 Headers 显示为左键?

提问于
浏览
1

我的TabBarView中有一个navigationBar . 我想在我的navigationBar上有一个rightBarButtonItem和一个 Headers . 为什么会这样?我的代码在viewDidLoad方法中 . 为什么我的 Headers 显示为左键项目?然后,当我点击它时,它就像是在移动视图,然后我的右边的按钮项目消失了, Headers 是正确的 .

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];

2 回答

  • 4

    UINavigationItem包含 Headers 和条形按钮项 . 您正在创建多个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 .

  • 0

    您正在创建多个导航按钮 . 您应该将所有这些属性设置为单个按钮 . 如果要为栏设置 Headers ,可以将其设置为self.title = @“title”;

    您也可以尝试设置navigationbar.titleview .

相关问题