Ios UIPageViewController在UIPageViewControllerTransitionStyle.Scroll时忽略约束
我正在为我的烹饪博客开发一个应用程序,希望在苹果AppStore应用程序上实现一种外观和感觉。在我的仪表板ViewController:UIViewController类中,我添加了一个headerView:UIView和一个scrollView:UIScrollView。headerView由my HeaderPageViewController:UIPageViewController填充,如下所示。设置约束以实现滚动时的视差效果。 要获取视图中的某些内容,my HeaderPageViewController的视图将获取一个imageContentView:UIImageView,该视图在运行时使用当月食谱填充。当然,我向imageContentView添加了常规约束,因此它的大小与HeaderPageViewController.view相同 使用Ios UIPageViewController在UIPageViewControllerTransitionStyle.Scroll时忽略约束,ios,xcode,swift,uiscrollview,uipageviewcontroller,Ios,Xcode,Swift,Uiscrollview,Uipageviewcontroller,我正在为我的烹饪博客开发一个应用程序,希望在苹果AppStore应用程序上实现一种外观和感觉。在我的仪表板ViewController:UIViewController类中,我添加了一个headerView:UIView和一个scrollView:UIScrollView。headerView由my HeaderPageViewController:UIPageViewController填充,如下所示。设置约束以实现滚动时的视差效果。 要获取视图中的某些内容,my HeaderPageVie
HeaderPageViewController(transitionStyle:UIPageViewControllerTransitionStyle.PageCurl,navigationOrientation:UIPageViewControllerNavigationOrientation.Horizontal,options:nil)初始化HeaderPageViewController时
一切正常。imageContentView的约束将被应用,图像将随着其在headerView中的ViewController而增长和收缩。而当我使用HeaderPageViewController(transitionStyle:UIPageViewControllerTransitionStyle.Scroll,navigationOrientation:UIPageViewControllerNavigationOrientation.horizone,options:nil)初始化HeaderPageViewController时,
图像内容视图不会随着headerpiew的大小而改变
在下图的左侧,您可以看到使用带有UIPageViewControllerTransitionStyle.PageCurl的初始化器的效果。滚动时,headerPageViewController的大小将更改。在右侧,您可以看到设置UIPageViewControllerTransitionStyle时的结果。在初始化器中滚动。我更改了HeaderPageViewController的BackgroundColor属性,因此您可以更好地看到效果
我想我可以把它归结为UIPageViewController的分页。显示分页时,黑色区域立即可见,如下图所示显示分页。当向上滚动包含分页的视图时,会使用我的约束进行缩放,但显然这与我应用约束的视图不同 这是DashboardViewController的重要部分,包括myUIPageViewControllerDataSource函数声明
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
}
}
如果有人提示我必须应用约束的视图或我犯了愚蠢错误的地方,请告诉我。我的一位朋友提到了,它假设UINavigationController中的UIPageViewController不会更新底部布局的自动布局约束。听起来很可能,这个bug也会影响我的布局,即使它在
UIPageViewControllerTransitionStyle.PageCurl
上运行良好。这个bug现在在Apple中报告。我的一个朋友提到了,它假设UINavigationController中的UIPageViewController不会更新底部布局的自动布局约束。听起来有可能,这个bug也会影响我的布局,即使它在UIPageViewControllerTransitionStyle.PageCurl中运行良好。这个bug现在在Apple中报告。
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) {
}
}