Houman
Houman

Reputation: 66320

RightBarButtonItem shows only as inactive

I have a strange problem when trying to display a refresh button as the rightBarButtonItem.

In short, I have implemented it, but couldn't see anything when running the app. However when I click on the storyboard Debug --> View Debugging --> Capture View Hierarchy. I can see a refresh button that seems inactive and hidden. I have no idea why.

enter image description here

The viewcontrol is actually pushed in via a custom 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;
}

What am I missing please?

Upvotes: 3

Views: 87

Answers (3)

Houman
Houman

Reputation: 66320

I finally found the problem. What is very confusing in this scenario is the fact that the actual TNViewcontroller is wrapped inside a TNPViewController. The latter is the PageViewController. I kept trying to add the navigationItem on the TNViewController instead of the TNPViewController.

This is the correct way and it works.

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

Upvotes: 0

Steve Trombley
Steve Trombley

Reputation: 71

Did you try to add it after you instantiated the UINavigation Controller?

Upvotes: 0

Steve Trombley
Steve Trombley

Reputation: 71

Because you are adding it to a uiviewcontroller you need to create a UINavigationBar, a UINavigationItem and the UIButton. You then add the UIButton to the UINavigationItem and then add the UINavigationItem to the 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]];

This code comes the the answer found here:

adding barButtonItems to a UINavigationBar without a Navigation Controller

Hope this helps.

Upvotes: 1

Related Questions