将页面控件设置到 UIPageViewController 的顶部
Posted
技术标签:
【中文标题】将页面控件设置到 UIPageViewController 的顶部【英文标题】:Set page control to the top of UIPageViewController 【发布时间】:2018-04-07 16:51:43 【问题描述】:目前我想使用 UIPageViewController。但默认情况下,页面控件会自动放置在屏幕底部。那么我怎样才能实现如下图所示页面控件位于顶部的位置
我使用容器视图并将视图控制器更改为 uipageviewcontroller
更新
class ProfilePageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
var pageControl = UIPageControl(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 50))
lazy var VCArr: [UIViewController] =
return [self.VCInstance(name: "button1"),
self.VCInstance(name: "button2"),
self.VCInstance(name: "button3")]
()
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
for view in self.view.subviews
if view is UIScrollView
view.frame = UIScreen.main.bounds
else if view is UIPageControl
view.backgroundColor = UIColor.clear
private func VCInstance(name: String) -> UIViewController
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
override func viewDidLoad()
super.viewDidLoad()
self.dataSource = self
self.delegate = self
pageControl.currentPageIndicatorTintColor = UIColor.green
pageControl.pageIndicatorTintColor = UIColor.gray
if let firstVC = VCArr.first
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = VCArr.index(of: viewController) else
return nil
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else
return VCArr.last
//uncomment atas comment bawa untuk loop
//return nil
guard VCArr.count > previousIndex else
return nil
return VCArr[previousIndex]
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
guard let viewControllerIndex = VCArr.index(of: viewController) else
return nil
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCArr.count else
return VCArr.first
//uncomment atas, comment bawah untuk loop
//return nil
guard VCArr.count > nextIndex else
return nil
return VCArr[nextIndex]
public func presentationCount(for pageViewController: UIPageViewController) -> Int
return VCArr.count
public func presentationIndex(for pageViewController: UIPageViewController) -> Int
guard let firstViewController = viewControllers?.first,
let firstViewControllerIndex = VCArr.index(of: firstViewController) else
return 0
return firstViewControllerIndex
我已经更新了我已经做的最新代码,但页面控件似乎仍在页面底部。
下面是我当前页面控件的图片
【问题讨论】:
【参考方案1】:不要从外观上使用默认的 UIPageControl。删除那些块。 根据您的视图矩形和位置创建一个新的 UIPageControl。
在 UIpagecontrol 的框架中:设置 y = 0 表示顶部或 UIScreen.main.bounds.maxY - 偏移量
这里是示例代码
//1 : member variable
var pageControl = UIPageControl()
// Call it from viewDidLoad
func setupPageControl()
// 2: set y = 0 means top or UIScreen.main.bounds.maxY - offset
pageControl = UIPageControl(frame: CGRect(x: 0,y: 0,width: UIScreen.main.bounds.width,height: 50))
self.pageControl.numberOfPages = pageCount
// self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.lightGray
self.pageControl.pageIndicatorTintColor = UIColor.lightGray
self.pageControl.currentPageIndicatorTintColor = UIColor.black
pageControl.backgroundColor = UIColor.clear
self.view.addSubview(pageControl)
您需要从 viewControllerAfter () & viewControllerBefore
更新当前页面 (pageControl.currentPage = count
)
【讨论】:
我已经在页面控件上添加了 cgrect,但它仍然出现在页面底部 在viewdidLoad中,添加self.view.addSubview(pageControl) 添加到子视图后告诉我结果。以上是关于将页面控件设置到 UIPageViewController 的顶部的主要内容,如果未能解决你的问题,请参考以下文章