ios – 如何在导航栏中添加页面指示器?

喜欢twitter做的:

我在UITabBarController内部有一个PageViewController,而UITabBarController又在一个UINavigationController内。

任何人都可以告诉我如何在导航栏中显示页面指示器?

编辑:我只是想出了navigationController.viewController只包含堆栈。
我会在一分钟后发布一个编辑

编辑2:嗯,看来你必须先了解视图控制器的数量。

也许不是最好的解决方案,但它适用于我。刚刚试过:)

@interface ViewController () <UINavigationControllerDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINavigationController *navController = self.navigationController;
    navController.delegate = self;

    navController.navigationBar.barTintColor = [UIColor colorWithRed:.2
                                                               green:.4
                                                                blue:.9
                                                               alpha:1];

    CGSize navBarSize = navController.navigationBar.bounds.size;
    CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y,
                                                                       0, 0)];

    //Or whatever number of viewcontrollers you have
    [self.pageControl setNumberOfPages:2];

    [navController.navigationBar addSubview:self.pageControl];

    navController.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    int index = [navigationController.viewControllers indexOfObject:viewController];
    self.pageControl.currentPage = index;
}

@end

这里有一些截图。

翻译自:https://stackoverflow.com/questions/21073891/how-to-add-a-page-indicator-inside-the-navigation-bar

转载注明原文:ios – 如何在导航栏中添加页面指示器?