gpt4 book ai didi

ios - 在 Swift 3 中从 UIPageViewController 中删除黑边

转载 作者:行者123 更新时间:2023-11-30 13:02:22 27 4
gpt4 key购买 nike

我正在使用 Swift 3.0,并创建了一个 UIPageViewController,当前有两个页面(每个页面都是一个 UIViewController)。当我运行应用程序时,一切正常,除了显示 ViewController 时应用程序的底部和右侧有黑色空间。我也看不到 PageViewController 中的页面点。我已经实现了在屏幕上放置点的功能:

func pageViewController(pageViewController: PageViewController,
didUpdatePageCount count: Int)

func pageViewController(pageViewController: PageViewController,
didUpdatePageIndex index: Int)

布局 Layout

PageViewController 内的 View Controller VC in PVC

PageViewController 设置 PageViewController settings

有人知道如何修复页面周围的黑色边框并添加页面点吗?

<小时/>

更新

    import UIKit

class PageViewController: UIPageViewController {

weak var pageDelegate: PageViewControllerDelegate?

private(set) lazy var orderedViewControllers: [UIViewController] = {
// The view controllers will be shown in this order
return [self.newViewController(name: "view1"),
self.newViewController(name: "view2"),
self.newViewController(name: "view3"),
self.newViewController(name: "view4"),
self.newViewController(name: "view5")]
}()

override func viewDidLoad() {
super.viewDidLoad()

dataSource = self
delegate = self

if let initialViewController = orderedViewControllers.first {
scrollToViewController(viewController: initialViewController, direction: UIPageViewControllerNavigationDirection.forward)
}

pageDelegate?.pageViewController(pageViewController: self, didUpdatePageCount: orderedViewControllers.count)

}

/**
Scrolls to the next view controller.
*/
func scrollToNextViewController() {
if let visibleViewController = viewControllers?.first,
let nextViewController = pageViewController(self, viewControllerAfter: visibleViewController) {
scrollToViewController(viewController: nextViewController, direction: UIPageViewControllerNavigationDirection.forward)
}
}

/**
Scrolls to the view controller at the given index. Automatically calculates
the direction.

- parameter newIndex: the new index to scroll to
*/
func scrollToViewController(index newIndex: Int) {
if let firstViewController = viewControllers?.first,
let currentIndex = orderedViewControllers.index(of: firstViewController) {
let direction: UIPageViewControllerNavigationDirection = newIndex >= currentIndex ? .forward : .reverse
let nextViewController = orderedViewControllers[newIndex]
scrollToViewController(viewController: nextViewController, direction: direction)
}
}

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

/**
Scrolls to the given 'viewController' page.

- parameter viewController: the view controller to show.
*/
private func scrollToViewController(viewController: UIViewController,
direction: UIPageViewControllerNavigationDirection = .forward) {
setViewControllers([viewController],
direction: direction,
animated: true,
completion: { (finished) -> Void in
// Setting the view controller programmatically does not fire
// any delegate methods, so we have to manually notify the
// 'pageDelegate' of the new index.
self.notifyPageDelegateOfNewIndex()
})
}

/**
Notifies '_pageDelegate' that the current page index was updated.
*/
func notifyPageDelegateOfNewIndex() {
if let firstViewController = viewControllers?.first,
let index = orderedViewControllers.index(of: firstViewController) {
self.pageDelegate?.pageViewController(pageViewController: self, didUpdatePageIndex: index)
}
}

}

// MARK: UIPageViewControllerDataSource

extension PageViewController: UIPageViewControllerDataSource {

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

let previousIndex = viewControllerIndex - 1

// User is on the first view controller and swiped left to loop to
// the last view controller.
guard previousIndex >= 0 else {
return orderedViewControllers.last
}

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

// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else {
return orderedViewControllers.first
}

guard orderedViewControllersCount > nextIndex else {
return nil
}

return orderedViewControllers[nextIndex]
}

}

extension PageViewController: UIPageViewControllerDelegate {

func pageViewController(_ pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool) {
notifyPageDelegateOfNewIndex()
}

}

protocol PageViewControllerDelegate: class {

/**
Called when the number of pages is updated.

- parameter pageViewController: the PageViewController instance
- parameter count: the total number of pages.
*/
func pageViewController(pageViewController: PageViewController,
didUpdatePageCount count: Int)

/**
Called when the current index is updated.

- parameter pageViewController: the PageViewController instance
- parameter index: the index of the currently visible page.
*/
func pageViewController(pageViewController: PageViewController,
didUpdatePageIndex index: Int)

}

最佳答案

我现在遇到了同样的问题,我刚刚通过在 viewDidLoad() 函数中将背景颜色更改为白色来解决该问题

self.view.backgroundColor = .white

关于ios - 在 Swift 3 中从 UIPageViewController 中删除黑边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39779240/

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