Alex
Alex

Reputation: 21

Swift Nav Bar with Title and Page Control

I am developing a swift app but having some problem on putting title and page control, together on the nav bar. I only manage to put either one of them on the nav bar, but not both. I am hoping for something like this(as well as the animation when swiping to another view controller) :

enter image description here

Below is my screen output. Page controller(embedded in Navigation controller) is working, just wanted to add titles(change base on view) and page control.

enter image description here

And here's how I create the title:

This is how I create the title:

@IBOutlet weak var navBar: UINavigationItem!

let title: UILabel = UILabel(frame: CGRectMake(0, 0, 150, 44))
title.numberOfLines = 2
title.textAlignment = .Center
title.text = "News\n"
navBar.titleView = title

I am making the title into 2 lines to leave room for the page control. However, when I try to create page control programmatically, it appears to be "behind" the nav bar.

This is how create page control:

let pageControl : UIPageControl = UIPageControl(frame: CGRectMake(0, 0, 150, 44))
self.pageControl.numberOfPages = 3
self.pageControl.currentPage = 0
self.view.addSubview(pageControl)

Upvotes: 2

Views: 1016

Answers (1)

Wain
Wain

Reputation: 119031

Add both your label and page control to another view, setting the constraints / frames appropriately, then add that view as the titleView for the nav bar.

Upvotes: 1

Related Questions