簡體   English   中英

UIPageViewController的NavigationBar不顯示標題

[英]NavigationBar of UIPageViewController not displaying title

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

感謝幫助。

這可能是因為您創建了navBar,將其添加到子視圖,但更改了self.navigationItem 正如我在您的代碼中看到的,這些是不同的導航欄。 嘗試將navBar保存到某個strong屬性並更改navBar而不是self.navigationItem

PS在這一行中self.navigationItem.title = modelArray; 您為NSString分配NSMutableArray。 那很糟 :)

如果你只看到橙色的navBar ,那么你的self.navigationItem就不顯示了。 你需要你的viewControllernavigationController的孩子,就像那樣: 在此輸入圖像描述

如果你這樣做,那么你可以寫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

然后改變navItem title屬性。

很簡單。

這樣做吧

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

希望它能幫到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM