带有标题和页面控件的Swift导航条

时间:2022-12-05 23:18:26

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) :

我正在开发一个swift应用程序,但是在导航条上把标题和页面控件放在一起有问题。我只把其中一个放在导航条上,但不是两个。我希望有这样的东西(以及切换到另一个视图控制器时的动画):

带有标题和页面控件的Swift导航条

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.

下面是我的屏幕输出。页面控制器(嵌入导航控制器)正在工作,只想添加标题(根据视图更改)和页面控件。

带有标题和页面控件的Swift导航条

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)

1 个解决方案

#1


1  

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.

将您的标签和页面控件添加到另一个视图中,适当地设置约束/框架,然后将该视图添加到导航条的titleView中。

#1


1  

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.

将您的标签和页面控件添加到另一个视图中,适当地设置约束/框架,然后将该视图添加到导航条的titleView中。