ios - Auto Layout for UIScrollView w/ 2 Child Views -
i have scroll view that's set inside storyboard auto layout constraints fills entire view. scroll view contains 2 views managed uiviewcontrollers instantiated nibs. code include these subviews:
- (void)viewdidload { [super viewdidload]; contentviewcontroller *contentvc = [contentviewcontroller new]; [[nsbundle mainbundle] loadnibnamed:@"contentview" owner:contentvc options:nil]; self.scrollview.delegate = self; [self addchildviewcontroller:contentvc]; [self.scrollview addsubview:contentvc.contenttc.view]; [contentvc didmovetoparentviewcontroller:self]; [self addchildviewcontroller:contentvc.contenttc]; self.view.layer.bordercolor = [[uicolor blackcolor] cgcolor]; self.view.layer.borderwidth = 5.0; self.scrollview.layer.bordercolor = [[uicolor bluecolor] cgcolor]; self.scrollview.layer.borderwidth = 5.0; cgrect adminframe = contentvc.view.frame; adminframe.origin.x = adminframe.size.width; shareviewcontroller *shareviewcontroller = [[shareviewcontroller alloc] initwithnibname:@"shareview" bundle:nil]; [self addchildviewcontroller:shareviewcontroller]; [self.scrollview addsubview:shareviewcontroller.view]; shareviewcontroller.view.frame = adminframe; cgrect shareframe = shareviewcontroller.view.frame; shareframe.origin.x = shareframe.size.width; [self.view addsubview:self.scrollview]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:self.scrollview attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributeright multiplier:1.0 constant:0]]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:self.scrollview attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributebottom multiplier:1.0 constant:0]]; // 4) set size of scroll view contains frames cgfloat scrollwidth = 2 * self.view.frame.size.width; cgfloat scrollheight = self.view.frame.size.height; self.scrollview.contentsize = cgsizemake(scrollwidth, scrollheight); self.scrollview.pagingenabled = yes; self.scrollview.showshorizontalscrollindicator = no; self.scrollview.bounces = no;
don't have ability post image, here's link looks like:
dropbox.com/s/e84ya3ffrdzytac/screenshot%202015-08-19%2011.52.15.png
in iphone 6+ simulator, scrollview smaller in both width , height window, there's white space on right , bottom.
in iphone 4s simulator, width correct, subviews longer window, means they'll scroll down. works in iphone 5, though of views inferred , not set 4-inch screens.
apologies if redundant question, i've been searching through , banging head against wall 2 days now, appreciated.
** edit: updated code , behavior: **
- (void)viewdidload { [super viewdidload]; contentviewcontroller *contentvc = [contentviewcontroller new]; [[nsbundle mainbundle] loadnibnamed:@"contentview" owner:contentvc options:nil]; self.scrollview.delegate = self; self.scrollview.pagingenabled = yes; self.scrollview.showshorizontalscrollindicator = no; self.scrollview.bounces = no; self.scrollview.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:self.scrollview]; uiview *contentview = [[uiview alloc] initwithframe:cgrectmake(0, 0, self.view.frame.size.width * 2, self.view.frame.size.height)]; [self.scrollview addsubview:contentview]; [self addchildviewcontroller:contentvc]; [contentview addsubview:contentvc.contenttc.view]; [contentvc didmovetoparentviewcontroller:self]; [self addchildviewcontroller:contentvc.contenttc]; cgrect adminframe = contentvc.view.frame; adminframe.origin.x = adminframe.size.width; shareviewcontroller *shareviewcontroller = [[shareviewcontroller alloc] initwithnibname:@"shareview" bundle:nil]; [self addchildviewcontroller:shareviewcontroller]; [contentview addsubview:shareviewcontroller.view]; shareviewcontroller.view.frame = adminframe; self.view.layer.bordercolor = [[uicolor blackcolor] cgcolor]; self.view.layer.borderwidth = 3; self.scrollview.layer.bordercolor = [[uicolor bluecolor] cgcolor]; self.scrollview.layer.borderwidth = 5; contentview.layer.bordercolor = [[uicolor greencolor] cgcolor]; contentview.layer.borderwidth = 7; cgrect shareframe = shareviewcontroller.view.frame; shareframe.origin.x = shareframe.size.width; [self.scrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[v]|" options:0 metrics:nil views:@{@"v" : contentview}]]; [self.scrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[v]|" options:0 metrics:nil views:@{@"v" : contentview}]];
results: dropbox.com/s/29zwwn6inmdvz0e/screenshot%202015-08-19%2014.27.47.png?dl=0 , dropbox.com/s/rqsdk9yubq84ph6/screenshot%202015-08-19%2014.28.14.png?dl=0
here's image of constraints storyboard: dropbox.com/s/8c5wkfw81sqd8ij/screenshot%202015-08-19%2014.28.47.png?dl=0 (pinning edges superview)
bound scrollview left,right,top , bottom fine.
and like..
here code written yours
- (void)viewdidload
{
[super viewdidload]; uiviewcontroller *contentvc = [[uiviewcontroller alloc]init]; contentvc.view.backgroundcolor = [uicolor greencolor]; self.scrollview.delegate = self; [self addchildviewcontroller:contentvc]; [self.scrollview addsubview:contentvc.view]; [contentvc didmovetoparentviewcontroller:self]; [self addchildviewcontroller:contentvc]; self.view.layer.bordercolor = [[uicolor blackcolor] cgcolor]; self.view.layer.borderwidth = 5.0; self.scrollview.layer.bordercolor = [[uicolor bluecolor] cgcolor]; self.scrollview.layer.borderwidth = 5.0; cgrect adminframe = contentvc.view.frame; adminframe.origin.x = adminframe.size.width; uiviewcontroller *shareviewcontroller = [[uiviewcontroller alloc] init]; shareviewcontroller.view.backgroundcolor = [uicolor graycolor]; [self addchildviewcontroller:shareviewcontroller]; [self.scrollview addsubview:shareviewcontroller.view]; shareviewcontroller.view.frame = adminframe; cgrect shareframe = shareviewcontroller.view.frame; shareframe.origin.x = shareframe.size.width; [self.view addsubview:self.scrollview]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:self.scrollview attribute:nslayoutattributeright relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributeright multiplier:1.0 constant:0]]; [self.view addconstraint:[nslayoutconstraint constraintwithitem:self.scrollview attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:self.view attribute:nslayoutattributebottom multiplier:1.0 constant:0]]; // 4) set size of scroll view contains frames cgfloat scrollwidth = 2 * self.view.frame.size.width; cgfloat scrollheight = self.view.frame.size.height; self.scrollview.contentsize = cgsizemake(scrollwidth, scrollheight); self.scrollview.pagingenabled = yes; self.scrollview.showshorizontalscrollindicator = no; self.scrollview.bounces = no;
}
Comments
Post a Comment