[英]Set page control to the top of UIPageViewController
Currently i want to use UIPageViewController. 目前,我想使用UIPageViewController。 But by default the page control is automatic place on the bottom of screen.
但是默认情况下,页面控件是自动放置在屏幕底部的。 so how can i achieve like the image below where the page control is on the top
所以我怎么能像下面的图像一样实现页面控件在顶部
i use container view and change the view controller to uipageviewcontroller 我使用容器视图并将视图控制器更改为uipageviewcontroller
UPDATE 更新
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
}
}
i have already updated the latest code what i had already did, but the page control seem still at the bottom of page. 我已经更新了最新的代码,但是页面控件似乎仍在页面底部。
below is the image of my current page control 以下是我当前页面控件的图像
Don't use the default UIPageControl from appearance. 不要从外观上使用默认的UIPageControl。 Remove those block.
删除那些块。 Create a new UIPageControl based on your view rect and position.
根据您的视图矩形和位置创建一个新的UIPageControl。
In frame of UIpagecontrol: set y = 0 means top or UIScreen.main.bounds.maxY - offset
在UIpagecontrol的框架中:设置y = 0表示top或UIScreen.main.bounds.maxY-偏移量
Here is the sample code 这是示例代码
//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)
}
You need to update the current page ( pageControl.currentPage = count
) from viewControllerAfter () & viewControllerBefore 您需要从viewControllerAfter ()和viewControllerBefore更新当前页面(
pageControl.currentPage = count
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.