如何在导航栏中添加页面指示器? [英] How to add a page indicator inside the navigation bar?

查看:121
本文介绍了如何在导航栏中添加页面指示器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像推特一样:

我在UITabBarController里面有一个PageViewController,而UITabBarController又在UINavigationController里面。

I have a PageViewController inside a UITabBarController which in turn is inside a UINavigationController.

有谁知道我如何在导航栏中显示页面指示器?

Could anyone tell me how to show the page indicator inside the navigation bar?

推荐答案

编辑:我只知道navigationController.viewControllers只包含叠加。
我将在一分钟内发布一个编辑

I just figured out that navigationController.viewControllers only contains the stack. I will post an edit in a minute

编辑2:好吧,看来你必须事先知道视图控制器的数量。

Edit 2: Well, it seems that you have to know the number of view controllers before hand.

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

Maybe not the best solution, but it works for me. Just tried :)

@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

以下是一些截图。


这篇关于如何在导航栏中添加页面指示器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆