gpt4 book ai didi

swift - 在容器内移动页面控件

转载 作者:行者123 更新时间:2023-11-30 13:16:54 24 4
gpt4 key购买 nike

我目前有一个主视图 Controller ,其中有一个容器。该容器包含一个 pageviewcontroller 以及它使用的 3 个不同的 View Controller 。每一种都有自己的颜色。绿色蓝色和粉色。一切都很实用,但我希望这些点出现在彩色框架内而不是外部。如何更改页面控件的位置?

  import Foundation
import UIKit
class TestPageController: UIPageViewController{

private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController(color: "Green"),
self.newColoredViewController(color: "Blue"),
self.newColoredViewController(color: "Pink")]
}()

private func newColoredViewController(color: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "\(color)ViewController")
}

override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
configPageControl()
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
}
func configPageControl(){
var pageController = UIPageControl.appearance()
pageController.backgroundColor = UIColor.clear()
pageController.pageIndicatorTintColor = UIColor.black()
pageController.currentPageIndicatorTintColor = UIColor.blue()
}

}




extension TestPageController: UIPageViewControllerDataSource{


func presentationCount(for pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}

func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}

let previousIndex = viewControllerIndex - 1

guard previousIndex >= 0 else {
return nil
}

guard orderedViewControllers.count > previousIndex else {
return nil
}

return orderedViewControllers[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}

let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count

guard orderedViewControllersCount != nextIndex else {
return nil
}

guard orderedViewControllersCount > nextIndex else {
return nil
}

return orderedViewControllers[nextIndex]
}

}

enter image description here enter image description here

最佳答案

为此,您需要使用 UIPageControl(并将其放置在您想要的位置并设置 numberOfPages 属性),然后使用 UIPageControllerDelegate 的委托(delegate)方法:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if finished && completed{
if let actualController = pageViewController.viewControllers?.first as? MyViewController{
self.pageControl.currentPage = actualController.index
}
}
}

关于swift - 在容器内移动页面控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38126968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com