导航栏中的页面指示器和标题iOS Objective C

Posted

技术标签:

【中文标题】导航栏中的页面指示器和标题iOS Objective C【英文标题】:Page indicator and title in Navigation Bar iOS Objective C 【发布时间】:2014-10-31 09:54:08 【问题描述】:

我想创建一个导航栏,其中我有一个 pageControl 以及导航栏中的标题,就像 twitter ios 应用程序包含的一样,我通过设置得到它们

self.navigationItem.Title = @"Home";

并通过创建UIPageControl的实例并将其中心设置为导航栏的中心并使用UINavigationController's委托实现其导航检测来实​​现PageControl。但问题是 pageControl 和标题相互重叠。我怎样才能这样设置标题应该出现在页面指示器的顶部。这里是定位pageControl的代码。

self.navigationController.delegate = self;  
CGSize navBarSize = self.navigationController.navigationBar.bounds.size;  
CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );  
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y,
                                                                   0, 0)];  
[self.pageControl setNumberOfPages:3];  
[self.navigationController.navigationBar addSubview:self.pageControl];

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

【问题讨论】:

你可以尝试创建一个 UIView 并添加带有标题和 UIPageControl 的 UILabel,然后在 navigationBar 中添加这个 UIView 【参考方案1】:

我想通了,两者(标题和 pageControl)相互重叠,所以我只是更改了 pageControl 的 Y 轴和

self.navigationController.delegate = self;  
CGSize navBarSize = self.navigationController.navigationBar.bounds.size;  
CGPoint origin = CGPointMake( navBarSize.width/2, navBarSize.height/2 );  
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(origin.x, origin.y+45,
                                                               0, 0)]; //Here added 45 to Y and it did the trick 
[self.pageControl setNumberOfPages:3];  
[self.navigationController.navigationBar addSubview:self.pageControl];

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

【讨论】:

以上是关于导航栏中的页面指示器和标题iOS Objective C的主要内容,如果未能解决你的问题,请参考以下文章

iOS 状态栏中的加载指示器

如何打开iOS状态栏中的网络指示灯? [复制]

导航栏中的 iOS 搜索栏

导航栏中的 UIButton 无法识别 iOS 11 中的点击

iOS 7 导航栏中的搜索栏

IOS - 垂直对齐导航栏中的后退按钮