3

尝试将刷新按钮显示为 rightBarButtonItem 时,我遇到了一个奇怪的问题。

简而言之,我已经实现了它,但是在运行应用程序时什么都看不到。但是,当我单击情节提要时Debug --> View Debugging --> Capture View Hierarchy。我可以看到一个似乎不活动且隐藏的刷新按钮。我不知道为什么。

在此处输入图像描述

viewcontrol 实际上是通过自定义的 pageviewcontroller 推入的。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    TNViewController *initialViewController = [self viewControllerAtIndex:currentIndex];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                       target:self action:@selector(refreshClicked:)];
    initialViewController.navigationItem.rightBarButtonItem = refreshButton;
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:initialViewController];

    NSArray *viewControllers = [NSArray arrayWithObject:navigationController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    for (UIView *subview in self.pageController.view.subviews) {
        if ([subview isKindOfClass:[UIPageControl class]]) {
            UIPageControl *pageControl = (UIPageControl *)subview;
            pageControl.pageIndicatorTintColor = [UIColor blackColor];
            pageControl.currentPageIndicatorTintColor = [utils colorFromHexString:@"#AA3635"];
            pageControl.numberOfPages = _news.count;
            pageControl.backgroundColor = [UIColor whiteColor];
        }
    }
    self.edgesForExtendedLayout = UIRectEdgeTop;
}

请问我错过了什么?

4

3 回答 3

1

因为要将它添加到 uiviewcontroller,所以需要创建 UINavigationBar、UINavigationItem 和 UIButton。然后将 UIButton 添加到 UINavigationItem,然后将 UINavigationItem 添加到 UINavigationBar:

   _navBar = [[UINavigationBar alloc] init];
[_navBar setFrame:CGRectMake(0,0,self.view.bounds.size.width,52)];
[self.view addSubview:_navBar];
UINavigationItem *navItem = [[UINavigationItem alloc]initWithTitle:@""];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClicked:)];
[navItem setLeftBarButtonItem:barButton];

[_navBar setItems:@[navItem]];

此代码来自此处找到的答案:

在没有导航控制器的情况下将 barButtonItems 添加到 UINavigationBar

希望这可以帮助。

于 2015-09-07T00:03:00.497 回答
0

实例化 UINavigation 控制器后是否尝试添加它?

于 2015-09-07T12:48:38.110 回答
0

我终于找到了问题所在。在这种情况下非常令人困惑的是实际TNViewcontroller包裹在TNPViewController. 后者是 PageViewController。我一直试图在TNViewController而不是在TNPViewController.

这是正确的方法并且有效。

父视图控制器:

TNPViewController *tnp = [[TNPViewController alloc] initWithNews:news index:indexPath.row];
[[self navigationController] pushViewController:tnp animated:YES];

TNPViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                                      target:self action:@selector(refreshClicked:)];
    self.navigationItem.rightBarButtonItem = refreshButton;
}
于 2015-09-07T18:54:58.893 回答