2014-10-31 67 views
0

我想創建一個導航欄,其中我有一個pageControl以及導航欄中的標題就像twitter iOS應用包含的一樣,我通過設置導航欄中的頁面指示器和標題iOS Objective C

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; } 
+1

你可以嘗試創建一個的UIView和添加到標題和UIPageControl的UILabel,然後將這個UIView添加到navigationBar中 – Ilario 2014-10-31 09:56:52

回答

0

我想通了,雙方(標題和的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; }