我有一个运行良好的页面视图控制器 - 我想拥有一个视图控制器但是在页面视图控制器中有多个页面 - 我的意思是当用户滚动可以再次看到该视图控制器并且在故事板中时只有一个视图调节器 这是我的代码工作但是当我想将页面数量更改为多个页面时,它无法正常工作 这是我的代码
class mainPage: UIPageViewController, UIPageViewControllerDataSource , UIPageViewControllerDelegate {
let pageVC3 = UIPageControl()
lazy var VCArr : [UIViewController] = {
return [self.VCInstance(name : "page")]
}()
private func VCInstance(name : String) -> UIViewController {
return UIStoryboard(name : "Main" , bundle : nil).instantiateViewController(withIdentifier: name)
}
override func viewDidLoad() {
super.viewDidLoad()
let pageControl = UIPageControl.appearance()
pageControl.layer.position.y = self.view.frame.height - 20
pageControl.pageIndicatorTintColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.3)
pageControl.currentPageIndicatorTintColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.6)
//Setting the background of the view controller so the dots wont be on a black background
self.view.backgroundColor = UIColor.black
self.dataSource = self
self.delegate = self
if let firstVC = VCArr.first {
setViewControllers([firstVC] , direction: .forward , animated: true, completion: nil)
setViewControllers([firstVC] , direction: .reverse , animated: true, completion: nil)
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for subV in self.view.subviews {
if type(of: subV).description() == "UIPageControl" {
let pos = CGPoint(x: 0, y: 600)
subV.frame = CGRect(origin: pos, size: subV.frame.size)
}
}
for view in self.view.subviews {
if view is UIScrollView {
view.frame = UIScreen.main.bounds
}else if view is UIPageControl{
view.backgroundColor = UIColor.clear
}
}
}
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 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 nil
}
guard VCArr.count > nextIndex else {
return nil
}
return VCArr[nextIndex]
}
public func presentationCount(for pageViewController: UIPageViewController) -> Int{
return 3
}
public func presentationIndex(for pageViewController: UIPageViewController) -> Int{
guard let firstViewController = viewControllers?.first , let firstViewControllerIndex = VCArr.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
答案 0 :(得分:0)
问题在于此代码
if let firstVC = VCArr.first {
setViewControllers([firstVC] , direction: .forward , animated: true, completion: nil)
setViewControllers([firstVC] , direction: .reverse , animated: true, completion: nil)
}
在pageController中添加相同的对象。
尝试创建不同的对象并添加到pageviewcontroller
let item1 = ViewControler()
let item 2 = ViewControler()
在PageController
中添加这两个对象答案 1 :(得分:0)
所以这是我找到的答案
我不得不使用这个
lazy var VCArr : [UIViewController] = {
return []
}()
然后使用此
var numberOfPages = 3
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...numberOfPages - 1 {
VCArr.append(self.VCInstance(name : "page"))
}
}
因此您可以看到页面视图控制器页面可以按页面数量更改