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

感谢帮助。

4

3 回答 3

1

这可能是因为您创建了 navBar,将其添加到子视图,但更改了self.navigationItem. 正如我在您的代码中看到的,这些是不同的导航栏。尝试将您保存navBar到一些strong属性中并更改navBar而不是self.navigationItem

PS 在这一行self.navigationItem.title = modelArray;中,您将 NSMutableArray 分配给 NSString。那很糟 :)

于 2013-07-02T15:23:32.400 回答
1

很简单。

这样做

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

希望它可以帮助你。

于 2013-07-02T19:14:42.680 回答
1

如果您只看到 orange navBar,那么您self.navigationItem只是没有显示。你需要你viewController成为这样的孩子navigationController在此处输入图像描述

如果你这样做,那么你可以self.navigationItem.title = @"123";在这里写: 在此处输入图像描述

你会得到你想要的结果: 在此处输入图像描述

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

之后更改title.navItem

于 2013-07-02T15:37:00.900 回答