How to add UIBarButtonItem to NavigationBar while using UIPageViewController

帅比萌擦擦* 提交于 2019-12-03 03:32:38

If you need the button to change each time you change the page you look at, it must be done in one of the delegate methods that you have tried. I suggest however, that instead of creating a new button and setting it to the navigation item, you simply change the title.

You can create the button in interface builder and link it to a property in your DetailViewController and then call setTitle:forState with UIControlStateNormal on the button every time you go to a new page.

If you need the button to do something different for each page, I would recommend checking the current page in the buttons action method rather than declaring a new one each time.

You can find the correct navigation item to set buttons/titles on by finding the page controller in one of its paged view controllers, then getting its nav item. e.g.

UIViewController *pageController = [[self.navigationController childViewControllers] lastObject];
pageController.navigationItem.title = @"My page's title";

It's really annoying that self.navigationItem doesn't work!

One of my colleagues experienced the same problem with page view controller.

As far as I understand, you wont' be able to add UIBarButtonItem to navigation bar of member view controllers in UIPageViewController. Reason being 'navigationController' is set to nil for UIPageViewController. To confirm, print value of [UIPageViewController navigationController].

However you can do one thing to overcome this issue. Please download example PhotoScroller.

And do following changes in AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
// kick things off by making the first page
PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];

UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:pageZero];
if (pageZero != nil)
{
    // assign the first page to the pageViewController (our rootViewController)
    UIPageViewController *pageViewController = (UIPageViewController *)self.window.rootViewController;
    pageViewController.dataSource = self;

    [pageViewController setViewControllers:@[nav]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:NULL];
}
return YES;
}

And following changes in the PhotoViewController.m

- (void)loadView 
{
.....
.....
self.title =@"Frogs";
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithTitle:@"Something" style:UIBarButtonItemStylePlain target:self action:@selector(doSomething)];

[newButton setTintColor:[UIColor redColor]];
self.navigationItem.rightBarButtonItem = newButton;
}

To avoid crash, you have to handle UIPageViewControllerDataSource delegate methods correctly in AppDelegate.m

Though, I wouldn't advice you to do above as it breaks the whole concept of UIPageViewController.

I hope this would be helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!