简体   繁体   English

UIPageViewController的NavigationBar不显示标题

[英]NavigationBar of UIPageViewController not displaying title

Navigation Bar of UIPageViewController is not displaying page numbers as title of nav bar. UIPageViewController的导航栏未显示页码作为导航栏的标题。

- (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 help. 感谢帮助。

It's probably because you create navBar, add it to subview, but changing self.navigationItem . 这可能是因为您创建了navBar,将其添加到子视图,但更改了self.navigationItem As I see in your code, these are different navigation bars. 正如我在您的代码中看到的,这些是不同的导航栏。 Try saving your navBar into some strong property and changing navBar instead of self.navigationItem 尝试将navBar保存到某个strong属性并更改navBar而不是self.navigationItem

PS In this line self.navigationItem.title = modelArray; PS在这一行中self.navigationItem.title = modelArray; you assigning an NSMutableArray to NSString. 您为NSString分配NSMutableArray。 That's bad :) 那很糟 :)

If you see only orange navBar , then your self.navigationItem just not showing. 如果你只看到橙色的navBar ,那么你的self.navigationItem就不显示了。 You need your viewController to be a child of navigationController like there: 你需要你的viewControllernavigationController的孩子,就像那样: 在此输入图像描述

If you do that, then you can write self.navigationItem.title = @"123"; 如果你这样做,那么你可以写self.navigationItem.title = @"123"; like here: 像这儿: 在此输入图像描述

and you will get result you want: 你会得到你想要的结果: 在此输入图像描述

UPD: Or you can do in that way: UPD:或者你可以这样做:

@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 title property of navItem . 然后改变navItem title属性。

It is simple. 很简单。

Do it this way 这样做吧

_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 it helps you. 希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM