UIPageViewController在UIPageViewControllerTransitionStyle.Scroll时忽略约束

时间:2015-07-14 08:19:22

标签: ios xcode swift uiscrollview uipageviewcontroller

我正在为我的烹饪博客开发一个应用程序,并希望在Apple AppStore应用程序上实现外观和感觉。在我的 DashboardViewController UIViewController 类中,我添加了 headerView UIView scrollView 的UIScrollView 即可。 headerView 填充了我的 HeaderPageViewController UIPageViewController ,如下所示。设置约束以实现滚动的视差效果。 View Hierarchy

要在Views中获取一些内容,我的 HeaderPageViewController 的View将获得一个 imageContentView UIImageView ,它在运行时填充了这个月。当然我在 imageContentView 中添加了通常的约束,因此它与 HeaderPageViewController.view 的大小相同!

使用HeaderPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.PageCurl, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)初始化 HeaderPageViewController 时,一切正常。将应用 imageContentView 的约束,并使用 headerView 中的ViewController增大和缩小图像。然而,当我使用HeaderPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)初始化 HeaderPageViewController 时, imageContentView 将不会随着 headerView 的大小而改变。

在下面图片的左侧,您可以使用带有 UIPageViewControllerTransitionStyle.PageCurl 的初始化程序查看效果。在滚动headerPageViewController的大小更改。在初始化程序中设置 UIPageViewControllerTransitionStyle.Scroll 时,您可以在右侧看到结果。我更改了 HeaderPageViewController 的BackgroundColor属性,因此您可以更好地查看效果。

Scrolling

我想我可以把它归结为 UIPageViewController 的分页。当显示分页时,黑色区域立即可见,在其上显示分页,如下所示。当向上滚动包含分页的视图时,我的约束会缩放,但显然这与我应用约束的视图不同。

Pagination

这是 DashboardViewController 的重要部分,包括我的 UIPageViewControllerDataSource 函数声明。

class DashboardViewController: UIViewController, NSFetchedResultsControllerDelegate, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    [...]

    func populateHeaderView() {
        let headerPageViewController:HeaderPageViewController = HeaderPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)
        //transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil
        headerPageViewController.view.backgroundColor = UIColor.blackColor()
        headerPageViewController.dataSource = self
        headerPageViewController.delegate = self
        let currentViewController:[UIViewController] = [self.viewControllerAtIndex(0)!]
        headerPageViewController.setViewControllers(currentViewController, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)

        self.addChildViewController(headerPageViewController)
        let headerPageView:UIView = headerPageViewController.view
        headerPageView.translatesAutoresizingMaskIntoConstraints = false
        headerPageView.contentMode = UIViewContentMode.ScaleAspectFill
        headerView.addSubview(headerPageView)
        headerPageViewController.didMoveToParentViewController(self)

        var headerPageViewContraints = [NSLayoutConstraint]()
        headerPageViewContraints.append(NSLayoutConstraint(item: headerPageView,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: headerView,
            attribute:NSLayoutAttribute.Top,
            multiplier: 1.0,
            constant: 0.0))
        headerPageViewContraints.append(NSLayoutConstraint(item: headerPageView,
            attribute: NSLayoutAttribute.Bottom,
            relatedBy: NSLayoutRelation.Equal,
            toItem: headerView,
            attribute:NSLayoutAttribute.Bottom,
            multiplier: 1.0,
            constant: 0.0))
        headerPageViewContraints.append(NSLayoutConstraint(item: headerPageView,
            attribute: NSLayoutAttribute.Leading,
            relatedBy: NSLayoutRelation.Equal,
            toItem: headerView,
            attribute:NSLayoutAttribute.Leading,
            multiplier: 1.0,
            constant: 0.0))
        headerPageViewContraints.append(NSLayoutConstraint(item: headerPageView,
            attribute: NSLayoutAttribute.Trailing,
            relatedBy: NSLayoutRelation.Equal,
            toItem: headerView,
            attribute:NSLayoutAttribute.Trailing,
            multiplier: 1.0,
            constant: 0.0))
        self.view.addConstraints(headerPageViewContraints)
    }

    // MARK: UIPageViewController DataSource

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if var index:UInt = (viewController as! HeaderPageViewController).index {
            if (index == 0) {
                return nil
            }
            index--

            return self.viewControllerAtIndex(index)
        }
        return nil
    }

        func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        if var index:UInt = (viewController as! HeaderPageViewController).index {
            index++
            if (Int(index) == headerRecipes.count) {
                return nil
            }
            return self.viewControllerAtIndex(index)
        }
        return nil
    }

    /*
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return headerRecipes.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }
    */

    // MARK: UIPageViewController Helper

    func getReasonableRecipesForHeaderPageViewController() {
        if(headerRecipes.count == 0) {
            headerRecipes = fetchedResultsController.fetchedObjects as! [Posts]
            if (headerRecipes.count > 3) {
                headerRecipes = Array(headerRecipes[0..<3])
            }
        }
    }

    func viewControllerAtIndex(index:UInt) -> UIViewController? {
        if (headerRecipes.count == 0 || Int(index) >= headerRecipes.count) {
            return nil
        }

        let headerPageViewController:HeaderPageViewController = HeaderPageViewController()
        let recipe:Posts = headerRecipes[Int(index)] as Posts
        headerPageViewController.manageImageContentView(UIImageView(image: UIImage(data:recipe.featured_image!)))
        headerPageViewController.index = index
        return headerPageViewController
    }
}

还有完整的 HeaderPageViewController 类,它是 UIPageViewController 的子类:

import Foundation
import UIKit
class HeaderPageViewController: UIPageViewController {
    var imageView:UIImageView = UIImageView()
    var index:UInt?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func manageImageContentView(imageContentView:UIImageView) {
        imageView = imageContentView
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true

        self.view.addSubview(imageView)

        var headerViewContraints = [NSLayoutConstraint]()
        headerViewContraints.append(NSLayoutConstraint(item: imageView,
            attribute: NSLayoutAttribute.Top,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.topLayoutGuide,
            attribute:NSLayoutAttribute.Bottom,
            multiplier: 1.0,
            constant: 0.0))
        headerViewContraints.append(NSLayoutConstraint(item: imageView,
            attribute: NSLayoutAttribute.Bottom,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.view,
            attribute:NSLayoutAttribute.Bottom,
            multiplier: 1.0,
            constant: 0.0))
        headerViewContraints.append(NSLayoutConstraint(item: imageView,
            attribute: NSLayoutAttribute.Leading,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.view,
            attribute:NSLayoutAttribute.Leading,
            multiplier: 1.0,
            constant: 0.0))
        headerViewContraints.append(NSLayoutConstraint(item: imageView,
            attribute: NSLayoutAttribute.Trailing,
            relatedBy: NSLayoutRelation.Equal,
            toItem: self.view,
            attribute:NSLayoutAttribute.Trailing,
            multiplier: 1.0,
            constant: 0.0))
        self.view.addConstraints(headerViewContraints)
    }

    override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {

    }
}

如果你们中的任何一个人暗示我必须应用我的约束或我犯了一个愚蠢的错误,请告诉我。

0 个答案:

没有答案