objective c - UIScrollView showing only part of UIView after rotation -
this first post stack overflow, , i'm ios beginner, please bear me!
i have illustration app have 3 uiviews (headerview, scrollviewcontainer, , bodyview) in parent uiview (topview), , of these views created in code. topview added uiscrollview (pagescrollview) created in storyboard.
pagescrollview filling total screen of iphone, , used autolayout. app contains viewcontroller.h , companioning .m-file seen below, plus appdelegate.x. think used single view application template start with. i'm using ios 6 , xcode 4.6, tried 4.5.
i've tried remove much possible of other code that's irrelevant problem.
the problem: when app starts shows views correctly, , scrollview allows view 3 views intended. after rotating landscape, scrollview somehow offsets content. example: remain @ top , rotate = content looks ok, scroll downwards bit , rotate makes top of content not show.
what have tried: i've searched net help, haven't found seemed help me. i've added logging of various info origin, , contentsize, , tried set of them without success. used 'po [[uiwindow keywindow] _autolayouttrace]' ensure constraints ok.
i can't see i'm doing wrong. there obvious things missing in code?
thanks in advance!
here's viewcontroller.m:
#import "viewcontroller.h" @interface viewcontroller () { uiview *topview; uiview *headerview; uiview *bodyview; uiview *scrollviewcontainer; uiinterfaceorientation neworientation; cgfloat bodyviewheight; cgsize newbounds; float picturescrollheight; } @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; newbounds = [self sizeinorientation:[uiapplication sharedapplication].statusbarorientation]; neworientation = [uiapplication sharedapplication].statusbarorientation; bodyviewheight = 1200; // height of body view varies in size depending on orientation self.pagescrollview.translatesautoresizingmaskintoconstraints = no; topview = [[uiview alloc] init]; [topview setbackgroundcolor:[uicolor clearcolor]]; topview.translatesautoresizingmaskintoconstraints = no; [self.pagescrollview addsubview:topview]; headerview = [[uiview alloc] init]; [headerview setbackgroundcolor:[uicolor redcolor]]; headerview.translatesautoresizingmaskintoconstraints = no; [topview addsubview:headerview]; scrollviewcontainer = [[uiview alloc] init]; [scrollviewcontainer setbackgroundcolor:[uicolor bluecolor]]; scrollviewcontainer.translatesautoresizingmaskintoconstraints = no; [topview addsubview:scrollviewcontainer]; bodyview = [[uiview alloc] init]; [bodyview setbackgroundcolor:[uicolor greencolor]]; bodyview.translatesautoresizingmaskintoconstraints = no; [topview addsubview:bodyview]; [self updateviewconstraints]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; } - (void)updateviewconstraints { [super updateviewconstraints]; // remove old constraints [self.view removeconstraints:self.view.constraints]; [self.pagescrollview removeconstraints:self.pagescrollview.constraints]; [topview removeconstraints:topview.constraints]; [scrollviewcontainer removeconstraints:scrollviewcontainer.constraints]; if ((neworientation == uideviceorientationlandscapeleft) || (neworientation == uideviceorientationlandscaperight)) { picturescrollheight = 300; } else { picturescrollheight = 203; } [headerview setneedsdisplay]; [bodyview setneedsdisplay]; cgfloat topviewheight = bodyviewheight + 55 + picturescrollheight; //self.pagescrollview = _pagescrollview nsdictionary *viewsdict = nsdictionaryofvariablebindings(_pagescrollview, topview, headerview, bodyview, scrollviewcontainer); nsdictionary *metricsdict = @{@"topviewheight": [nsnumber numberwithfloat:topviewheight], @"newboundswidth": [nsnumber numberwithfloat:newbounds.width], @"picturescrollheight": [nsnumber numberwithfloat:picturescrollheight], @"bodyviewheight": [nsnumber numberwithfloat:bodyviewheight]}; // pagescrollview - kid self.view [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[_pagescrollview]-0-|" options:0 metrics:nil views:viewsdict]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-0-[_pagescrollview]-0-|" options:0 metrics:nil views:viewsdict]]; // topview - kid pagescrollview [self.pagescrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[topview(newboundswidth)]-0-|" options:0 metrics:metricsdict views:viewsdict]]; [self.pagescrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-0-[topview(topviewheight)]-0-|" options:0 metrics:metricsdict views:viewsdict]]; // headerview - kid topview [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[headerview]-0-|" options:0 metrics:nil views:viewsdict]]; [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-0-[headerview(55.0)]" options:0 metrics:nil views:viewsdict]]; // scrollviewcontainer - kid topview [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[scrollviewcontainer]-0-|" options:0 metrics:nil views:viewsdict]]; [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:[headerview]-0-[scrollviewcontainer(picturescrollheight)]" options:0 metrics:metricsdict views:viewsdict]]; // bodyview - kid topview [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[bodyview]-0-|" options:0 metrics:nil views:viewsdict]]; [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:[scrollviewcontainer]-0-[bodyview(bodyviewheight)]-0-|" options:0 metrics:metricsdict views:viewsdict]]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (void)willrotatetointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration { neworientation = tointerfaceorientation; newbounds = [self sizeinorientation:tointerfaceorientation]; } -(cgsize) sizeinorientation:(uiinterfaceorientation)orientation { cgsize size = [uiscreen mainscreen].bounds.size; uiapplication *application = [uiapplication sharedapplication]; if (uiinterfaceorientationislandscape(orientation)) { size = cgsizemake(size.height, size.width); } if (application.statusbarhidden == no) { size.height -= min(application.statusbarframe.size.width, application.statusbarframe.size.height); } homecoming size; } @end
first of not need recreate constraints in -updateviewconstraints method. need update them in place. accomplish aim following:
create constraints once. illustration in method -setupconstraints. , maintain reference need updated. see code below. in method -updateviewconstraints update topview height , width constraints , height scrollviewcontainer.here's viewcontroller.m should like:
#import "viewcontroller.h" @interface viewcontroller () @property (nonatomic, strong) iboutlet uiscrollview* pagescrollview; @property (nonatomic, strong) nslayoutconstraint* pictureheightconstraint; @property (nonatomic, strong) nslayoutconstraint* topviewwidthconstraint; @property (nonatomic, strong) nslayoutconstraint* topviewheightconstraint; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; newbounds = [self sizeinorientation:[uiapplication sharedapplication].statusbarorientation]; neworientation = [uiapplication sharedapplication].statusbarorientation; bodyviewheight = 1200; // height of body view varies in size depending on orientation self.pagescrollview.translatesautoresizingmaskintoconstraints = no; topview = [[uiview alloc] init]; [topview setbackgroundcolor:[uicolor clearcolor]]; topview.translatesautoresizingmaskintoconstraints = no; [self.pagescrollview addsubview:topview]; headerview = [[uiview alloc] init]; [headerview setbackgroundcolor:[uicolor redcolor]]; headerview.translatesautoresizingmaskintoconstraints = no; [topview addsubview:headerview]; scrollviewcontainer = [[uiview alloc] init]; [scrollviewcontainer setbackgroundcolor:[uicolor bluecolor]]; scrollviewcontainer.translatesautoresizingmaskintoconstraints = no; [topview addsubview:scrollviewcontainer]; bodyview = [[uiview alloc] init]; [bodyview setbackgroundcolor:[uicolor greencolor]]; bodyview.translatesautoresizingmaskintoconstraints = no; [topview addsubview:bodyview]; [self setupconstraints]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; } - (void)setupconstraints { // remove old constraints [self.view removeconstraints:self.view.constraints]; [self.pagescrollview removeconstraints:self.pagescrollview.constraints]; [topview removeconstraints:topview.constraints]; [scrollviewcontainer removeconstraints:scrollviewcontainer.constraints]; if ((neworientation == uideviceorientationlandscapeleft) || (neworientation == uideviceorientationlandscaperight)) { picturescrollheight = 300; } else { picturescrollheight = 203; } [headerview setneedsdisplay]; [bodyview setneedsdisplay]; cgfloat topviewheight = bodyviewheight + 55 + picturescrollheight; //self.pagescrollview = _pagescrollview nsdictionary *viewsdict = nsdictionaryofvariablebindings(_pagescrollview, topview, headerview, bodyview, scrollviewcontainer); // pagescrollview - kid self.view [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[_pagescrollview]-0-|" options:0 metrics:nil views:viewsdict]]; [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-0- [_pagescrollview]-0-|" options:0 metrics:nil views:viewsdict]]; // topview - kid pagescrollview [self.pagescrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[topview]-0-|" options:0 metrics:nil views:viewsdict]]; [self.pagescrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-0-[topview]-0-|" options:0 metrics:nil views:viewsdict]]; nslayoutconstraint* topviewwidthconstraint = [nslayoutconstraint constraintwithitem:topview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:newbounds.width]; self.topviewwidthconstraint = topviewwidthconstraint; [topview addconstraint:self.topviewwidthconstraint]; nslayoutconstraint* topviewheightconstraint = [nslayoutconstraint constraintwithitem:topview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:topviewheight]; self.topviewheightconstraint = topviewheightconstraint; [topview addconstraint:self.topviewheightconstraint]; // headerview - kid topview [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[headerview]-0-|" options:0 metrics:nil views:viewsdict]]; [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|-0-[headerview(55.0)]" options:0 metrics:nil views:viewsdict]]; // scrollviewcontainer - kid topview [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[scrollviewcontainer]-0-|" options:0 metrics:nil views:viewsdict]]; [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:[headerview]-0-[scrollviewcontainer]" options:0 metrics:nil views:viewsdict]]; nslayoutconstraint* pictureheightconstraint = [nslayoutconstraint constraintwithitem:scrollviewcontainer attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1 constant:picturescrollheight]; self.pictureheightconstraint = pictureheightconstraint; [scrollviewcontainer addconstraint:self.pictureheightconstraint]; // bodyview - kid topview [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-0-[bodyview]-0-|" options:0 metrics:nil views:viewsdict]]; [topview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v: [scrollviewcontainer]-0-[bodyview]-0-|" options:0 metrics:nil views:viewsdict]]; } - (void)updateviewconstraints { [super updateviewconstraints]; if ((neworientation == uideviceorientationlandscapeleft) || (neworientation == uideviceorientationlandscaperight)) { picturescrollheight = 300; } else { picturescrollheight = 203; } cgfloat topviewheight = bodyviewheight + 55 + picturescrollheight; self.pictureheightconstraint.constant = picturescrollheight; self.topviewheightconstraint.constant = topviewheight; self.topviewwidthconstraint.constant = newbounds.width; [self.pagescrollview setneedsupdateconstraints]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (void)willrotatetointerfaceorientation:(uiinterfaceorientation)tointerfaceorientation duration:(nstimeinterval)duration { newbounds = [self sizeinorientation:tointerfaceorientation]; } -(cgsize)sizeinorientation:(uiinterfaceorientation)orientation { cgsize size = [uiscreen mainscreen].bounds.size; uiapplication *application = [uiapplication sharedapplication]; if (uiinterfaceorientationislandscape(orientation)) { size = cgsizemake(size.height, size.width); } if (application.statusbarhidden == no) { size.height -= min(application.statusbarframe.size.width, application.statusbarframe.size.height); } homecoming size; } @end
scrollview calculates content size automatically depending on subviews constraints added it. off course of study works in autolayout enviroument.
objective-c ios6 autolayout
No comments:
Post a Comment