RightBarButtonItem仅显示为非活动状态

时间:2015-09-06 22:27:48

标签: ios uinavigationbar rightbarbuttonitem

尝试将刷新按钮显示为rightBarButtonItem时,我遇到一个奇怪的问题。

简而言之,我已经实现了它,但在运行应用程序时却看不到任何内容。但是,当我点击故事板Debug --> View Debugging --> Capture View Hierarchy时。我可以看到一个看似不活动和隐藏的刷新按钮。我不明白为什么。

enter image description here

实际上,viewcontrol是通过自定义的pageviewcontroller推送的。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    TNViewController *initialViewController = [self viewControllerAtIndex:currentIndex];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                       target:self action:@selector(refreshClicked:)];
    initialViewController.navigationItem.rightBarButtonItem = refreshButton;
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:initialViewController];

    NSArray *viewControllers = [NSArray arrayWithObject:navigationController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    for (UIView *subview in self.pageController.view.subviews) {
        if ([subview isKindOfClass:[UIPageControl class]]) {
            UIPageControl *pageControl = (UIPageControl *)subview;
            pageControl.pageIndicatorTintColor = [UIColor blackColor];
            pageControl.currentPageIndicatorTintColor = [utils colorFromHexString:@"#AA3635"];
            pageControl.numberOfPages = _news.count;
            pageControl.backgroundColor = [UIColor whiteColor];
        }
    }
    self.edgesForExtendedLayout = UIRectEdgeTop;
}

我错过了什么?

3 个答案:

答案 0 :(得分:1)

因为您要将它添加到uiviewcontroller,您需要创建UINavigationBar,UINavigationItem和UIButton。然后将UIButton添加到UINavigationItem,然后将UINavigationItem添加到UINavigationBar:

   _navBar = [[UINavigationBar alloc] init];
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
[self.view addSubview:_navBar];
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClicked:)];
[navItem setLeftBarButtonItem:barButton];

[_navBar setItems:@[navItem]];

此代码的答案如下:

adding barButtonItems to a UINavigationBar without a Navigation Controller

希望这有帮助。

答案 1 :(得分:0)

在实例化UINavigation控制器后,您是否尝试添加它?

答案 2 :(得分:0)

我终于找到了问题。在这种情况下非常令人困惑的是,实际TNViewcontroller包含在TNPViewController内。后者是PageViewController。我一直试图在TNViewController而不是TNPViewController上添加navigationItem。

这是正确的方法并且有效。

ParentViewController:

TNPViewController *tnp = [[TNPViewController alloc] initWithNews:news index:indexPath.row];
[[self navigationController] pushViewController:tnp animated:YES];

<强> TNPViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                      target:self action:@selector(refreshClicked:)];
    self.navigationItem.rightBarButtonItem = refreshButton;
}