ios - scrollView: how to create a pure auto layout scrollview with paging? -


can tell me did wrong here... - pin uiscrollview container view - pin subviews onto uiscrollview

after reading apple technote it, tried both hybrid method , pure auto layout method. hybrid method using nib works awful paging, looks big picture in scrollview, rather paged.

i created pure auto layout version in code, uiscrollview subview of uiview. time view stuck, , uiimage gigantic, full size:

    //scroll view     if (self.scrollview == nil)     {         self.scrollview = [[uiscrollview alloc] initwithframe:self.frame];          self.scrollview.translatesautoresizingmaskintoconstraints  = no;          [self.scrollview setclipstobounds:no];         [self.scrollview setpagingenabled:yes];          [self addsubview: self.scrollview];         [self addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:|[scrollview(300)]|"                                                  options:0 metrics:nil                                                    views:@{@"scrollview":self.scrollview}]];         [self addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"v:|[scrollview(300)]|"                                                  options:0 metrics:nil                                                    views:@{@"scrollview":self.scrollview}]]; 

-(void) createcontentview  {            (int i=0; i<self.pageimages.count; i++) {                    uilabel* toplabel = [[uilabel alloc] init];          toplabel.text = [nsstring stringwithformat:@"toplabel %d", i+1];          [toplabel sizetofit];                    [self.toplabelarray insertobject:toplabel atindex:i];          [self.scrollview addsubview:toplabel];          toplabel.translatesautoresizingmaskintoconstraints = no;                      uilabel* bottomlabel = [[uilabel alloc] init];          bottomlabel.text = [nsstring stringwithformat:@"bottomlabel %d", i+1];          [bottomlabel sizetofit];            [self.bottomlabelarray insertobject:bottomlabel atindex:i];          [self.scrollview addsubview:bottomlabel];          bottomlabel.translatesautoresizingmaskintoconstraints = no;            uibutton* button = [[uibutton alloc] init];          button.titlelabel.text = [nsstring stringwithformat:@"button %d", i+1];          [button sizetofit];            [self.buttonarray insertobject:button atindex:i];          [self.scrollview addsubview:button];          button.translatesautoresizingmaskintoconstraints = no;                    uiimageview* imageview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:[self.pageimages objectatindex:i]]];                    imageview.frame = cgrectmake(0,0,200,200);                    imageview.translatesautoresizingmaskintoconstraints = no;          imageview.contentmode = uiviewcontentmodescaleaspectfit;                        [self.pageviews insertobject:imageview atindex:i];                    [self.scrollview addsubview:imageview];            nsdictionary* viewsdictionary = @{@"toplabel":toplabel,                                            @"bottomlabel":bottomlabel,                                            @"button": button,                                            @"imageview": imageview                                            };            [self.scrollview addconstraints:           [nslayoutconstraint constraintswithvisualformat:@"v:|-(10)-[toplabel]-(10)-[imageview]-(10)-[bottomlabel]-(10)-|"                                                   options:0 metrics:nil                                                     views:viewsdictionary]];          [self.scrollview addconstraints:           [nslayoutconstraint constraintswithvisualformat:@"v:[button]-(10)-|"                                                   options:0 metrics:nil                                                     views:viewsdictionary]];                    if (i==0)          {              [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:|-(10)-[toplabel]"                                                   options:0 metrics:nil                                                         views:viewsdictionary]];                            [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:|-(10)-[imageview]"                                                       options:0 metrics:nil                                                         views:viewsdictionary]];                [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:|-(10)-[bottomlabel]-(10)-[button]"                                                       options:0 metrics:nil                                                         views:viewsdictionary]];          }          else if (i == self.pageimages.count)          {              [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:-(10)-[toplabel]-(10)-|"                                                       options:0 metrics:nil                                                         views:viewsdictionary]];              [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:-(10)-[imageview]-(10)-|"                                                       options:0 metrics:nil                                                         views:viewsdictionary]];                [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:-(10)-[bottomlabel]-(10)-[button]-(10)-|"                                                       options:0 metrics:nil                                                         views:viewsdictionary]];                    }          else          {              [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:[prevtoplabel]-(10)-[toplabel]"                                                       options:0 metrics:nil                                                         views:@{@"prevtoplabel": [self.toplabelarray objectatindex: i-1],                                                                  @"toplabel": toplabel                                                                     }]];                            [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:[previmageview]-(10)-[imageview]"                                                       options:0 metrics:nil                                                         views:@{@"previmageview": [self.pageviews objectatindex: i-1],                                                                 @"imageview": imageview                                                                 }]];                            [self.scrollview addconstraints:               [nslayoutconstraint constraintswithvisualformat:@"h:[prevbutton]-(10)-[bottomlabel]-(10)-[button]"                                                       options:0 metrics:nil                                                         views:@{@"prevbutton": [self.buttonarray objectatindex: i-1],                                                                 @"button":button,                                                                 @"bottomlabel": bottomlabel                                                                 }]];          }          [self.scrollview addconstraint:[nslayoutconstraint                                    constraintwithitem:toplabel                                    attribute:nslayoutattributecenterx                                    relatedby:nslayoutrelationequal                                    toitem:imageview                                    attribute:nslayoutattributecenterx                                    multiplier:1                                    constant:0.0]];                    [self.scrollview addconstraint:[nslayoutconstraint                               constraintwithitem:bottomlabel                               attribute:nslayoutattributecenterx                               relatedby:nslayoutrelationequal                               toitem:imageview                               attribute:nslayoutattributecenterx                               multiplier:0.8                               constant:0.0]];      //        [self.scrollview addconstraint:[nslayoutconstraint  //          

ok, there's few things wrong here:

else if (i == self.pageimages.count)

this clause never run, loop count set break when i == self.pageimages.count -1, here for (int i=0; i<self.pageimages.count; i++).

next

        [self.scrollview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:-(10)-[toplabel]-(10)-|"                                                  options:0 metrics:nil                                                    views:viewsdictionary]];         [self.scrollview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:-(10)-[imageview]-(10)-|"                                                  options:0 metrics:nil                                                    views:viewsdictionary]];          [self.scrollview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:-(10)-[bottomlabel]-(10)-[button]-(10)-|"                                                  options:0 metrics:nil                                                    views:viewsdictionary]]; 

these produce invalid constraints, @"h:-(10)-[bottomlabel].... not pinning furthest left constraint anything. visual format strings connect views need start or finish view. either subview ([subview]) or superview (|). fix this, need keep reference previous pages label in loop, , add beginning of vfl string. this

   @"h:[prevbottomlabel]-(10)-[bottomlabel]....     prevbottomlabel = bottomlabel;    // continue loop 

next:

   uiimageview* imageview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:[self.pageimages objectatindex:i]]];      imageview.frame = cgrectmake(0,0,200,200);      imageview.translatesautoresizingmaskintoconstraints = no;     imageview.contentmode = uiviewcontentmodescaleaspectfit; 

this not have desired effect, can't manually set frame, switch on autolayout. currently, frame value ignored, , height , width set intrinsic content size of image view, height , width of image. if want set height , width, need constraints, so:

   [imageview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:[imageview(200)]"                                                  options:0 metrics:nil                                                    views:viewsdictionary]];     [imageview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"v:[imageview(200)]"                                                  options:0 metrics:nil                                                    views:viewsdictionary]]; 

that should give pointers, there may other mistakes in there fixing these place start.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

c++ - No viable overloaded operator for references a map -

java - UML - How would you draw a try catch in a sequence diagram? -