NavigationBar UIPageViewController does not display title

The UIPageViewController navigation bar does not display page numbers as the title of the navigation bar.

- (void)viewDidLoad { [super viewDidLoad]; //modelArray holds the page numbers modelArray = [[NSMutableArray alloc] init]; for (int index = 1; index <= totalPages; index++) { [modelArray addObject:[NSString stringWithFormat:@"%i", index]]; } UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45)]; navBar.tintColor = [UIColor colorWithRed:243.0/255.0 green:164.0/255.0 blue:0.0/255.0 alpha:1.0]; self.navigationItem.title = modelArray; [self displayPageNumber:1]; thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil]; thePageViewController.delegate = self; thePageViewController.dataSource = self; thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument]; contentViewController.page = [modelArray objectAtIndex:0]; NSArray *viewControllers = [NSArray arrayWithObject:contentViewController]; [thePageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; [self addChildViewController:thePageViewController]; [self.view addSubview:thePageViewController.view]; thePageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [thePageViewController didMoveToParentViewController:self]; _toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 960, self.view.bounds.size.width, 45)]; _toolbar.barStyle = UIBarStyleBlackOpaque; _toolbar.tintColor = [UIColor colorWithRed:243.0/255.0 green:164.0/255.0 blue:0.0/255.0 alpha:1.0]; [self.view addSubview:_toolbar]; [self.view addSubview:navBar]; } - (void) pageViewController:(PageViewController *)pageViewController willTurnToPageAtIndex:(NSUInteger)pageIndex { [self displayPageNumber:pageIndex + 1]; } - (void) displayPageNumber:(NSUInteger)pageNumber { self.navigationItem.title = [NSString stringWithFormat: @"Page %u of %u", pageNumber, CGPDFDocumentGetNumberOfPages(PDFDocument)]; } 

Thanks for the help.

+4
source share
3 answers

It's simple.

Do it like that

 _navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 45)]; _navBar.tintColor = [UIColor colorWithRed:243.0/255.0 green:164.0/255.0 blue:0.0/255.0 alpha:1.0]; UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:[NSString stringWithFormat: @"Page %i of %i", pageNumber, CGPDFDocumentGetNumberOfPages(thePDF)]]; [_navBar pushNavigationItem:title animated:NO]; 

Hope this helps you.

+1
source

Probably because you are creating a navBar, add it to the subview, but change the self.navigationItem . As I see in your code, these are different navigation bars. Try saving navBar to some strong and change navBar instead of self.navigationItem

PS In this line self.navigationItem.title = modelArray; you assign an NSMutableArray to an NSString. This is bad:)

+1
source

If you see only orange navBar , then your self.navigationItem just does not appear. You need your viewController be a child of the navigationController , for example: enter image description here

If you do, you can write self.navigationItem.title = @"123"; , like here: enter image description here

and you will get the result you want: enter image description here

UPD: Or you can do this:

 @interface MCViewController () @property (strong, nonatomic) UINavigationItem *navItem; @property (strong, nonatomic) UINavigationBar *navBar; @end @implementation MCViewController - (void)viewDidLoad { [super viewDidLoad]; self.navItem = [[UINavigationItem alloc] initWithTitle:@"TEST"]; self.navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; self.navBar.items = @[self.navItem]; [self.view addSubview:self.navBar]; } @end 

And after that, change the title property of navItem .

+1
source

Source: https://habr.com/ru/post/1489297/


All Articles