ios - UIPageViewController ignores Constraints when UIPageViewControllerTransitionStyle.Scroll -
i'm working on app culinary blog , wanted achieve , feel on apple appstore application. in dashboardviewcontroller:uiviewcontroller class added headerview:uiview , scrollview:uiscrollview. headerview populated headerpageviewcontroller:uipageviewcontroller seen below. constraints set achieve parallax effect on scrolling.
to content inside views headerpageviewcontroller's view gets imagecontentview:uiimageview populated @ runtime recipes of month. of course added usual constraints imagecontentview, has same size headerpageviewcontroller.view!
when initialising headerpageviewcontroller headerpageviewcontroller(transitionstyle: uipageviewcontrollertransitionstyle.pagecurl, navigationorientation: uipageviewcontrollernavigationorientation.horizontal, options: nil)
works fine. constraints of imagecontentview applied , image grows , shrinks it's viewcontroller inside headerview. whereas when initialise headerpageviewcontroller headerpageviewcontroller(transitionstyle: uipageviewcontrollertransitionstyle.scroll, navigationorientation: uipageviewcontrollernavigationorientation.horizontal, options: nil)
imagecontentview won't change size of headerview.
on left side of image below can see effect using initialiser uipageviewcontrollertransitionstyle.pagecurl. on scrolling size of headerpageviewcontroller changes. on right side can see result when setting uipageviewcontrollertransitionstyle.scroll in initialiser. changed backgroundcolor property of headerpageviewcontroller, can better see effect.
i think can boil down pagination of uipageviewcontroller. when displaying pagination black area visible, showing pagination on seen below. when scrolling view contains pagination scales constraints, apparently not same view 1 applied constraints to.
this important part of dashboardviewcontroller including uipageviewcontrollerdatasource function declaration.
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)] posts headerpageviewcontroller.manageimagecontentview(uiimageview(image: uiimage(data:recipe.featured_image!))) headerpageviewcontroller.index = index return headerpageviewcontroller } }
also there complete headerpageviewcontroller class, subclasses 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) { } }
if of has hint view have apply constraints or made dumb mistake, please tell me.
Comments
Post a Comment