UIPageViewController的NavigationBar没有显示标题

4

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

感谢您的帮助。
3个回答

1
如果您只看到橙色的 navBar,那么您的 self.navigationItem 就没有显示。您需要将您的 viewController 设为 navigationController 的子视图控制器,像这样: enter image description here 如果您这样做了,就可以像这样编写 self.navigationItem.title = @"123";enter image description here 然后您会得到想要的结果: enter image description here 更新:或者您可以这样做:
@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

然后改变navItemtitle属性。

如何以编程方式实现 - user1120133
好的,您可以像这样使用您的控制器作为rootViewController创建导航控制器:http://d.pr/i/cUKY navigationControllerappDelegate中的一个strong属性。 - Renderhp

1

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

希望它能对你有所帮助。

1
可能是因为您创建了navBar,将其添加到子视图中,但更改的是self.navigationItem。从您的代码中可以看出,这些是不同的导航栏。尝试将您的navBar保存到一些strong属性中,并更改navBar而不是self.navigationItem。
附注:在这一行中,您将NSMutableArray分配给NSString。那很糟糕 :)

我刚刚尝试了这个,但仍然不起作用:self.navigationItem.title = [NSString stringWithFormat: @"第 %u 页,共 %u 页", pageNumber, CGPDFDocumentGetNumberOfPages(pdf)]; - user1120133
不,稍后试着按照我说的做:“尝试将你的 navBar 保存到某个 strong 属性中,并改变 navBar 而不是 self.navigationItem”。 - Renderhp
现在我有一个强大的navBar属性,并尝试像这样使用_navBar.title = [NSString stringWithFormat: @"Page %u of %u", pageNumber, CGPDFDocumentGetNumberOfPages(PDFDocument)];但是类型为UINavigationBar的对象中没有title属性。 - user1120133

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接