3

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?

Houman
  • 64,245
  • 87
  • 278
  • 460

3 Answers3

1

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.

Community
  • 1
  • 1
  • But I'm utilising a UINavigationController in first place right? Why should I add these manually? I still gave it a shot without any luck. – Houman Sep 07 '15 at 08:35
0

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

  • I guess you can't comment yet, because you are too new. Usual awkwardness of Stackoverflow for new comers. In your answer you haven't specified that I had to instantiate UINavigation Controller. What am I missing please? Feel free to edit the main answer and i'll see it. – Houman Sep 07 '15 at 16:13
0

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;
}
Houman
  • 64,245
  • 87
  • 278
  • 460