从 Swift 3 中的 UIPageViewController 中删除黑边

Posted

技术标签:

【中文标题】从 Swift 3 中的 UIPageViewController 中删除黑边【英文标题】:Remove Black Edges from UIPageViewController in Swift 3 【发布时间】:2016-09-29 20:16:25 【问题描述】:

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

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

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

布局

PageViewController 内部的视图控制器

PageViewController 设置

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


更新

    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)


【问题讨论】:

这些图片的模式是什么? 模式是什么意思?我在 iPhone 6S plus 上运行它 我的意思是图像视图的内容模式,在你的故事板中选择图像视图,导航到属性检查器,“模式”前面选择的是什么? 请告诉我该图像视图的限制是什么? 模式:缩放填充,约束为:imgur.com/a/LwbWH设置为填充整个窗口 【参考方案1】:

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

self.view.backgroundColor = .white

【讨论】:

以上是关于从 Swift 3 中的 UIPageViewController 中删除黑边的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Swift 3 中的闭包中返回

从 Swift 3 中的 UIPageViewController 中删除黑边

如何从 Swift 3 中的数据中读取属性列表

无法从同一类中的闭包中更改变量 [swift 3.0]

从 Swift 3 中的另一个项目访问视图控制器

从firebase检索到swift 3中的多个按钮