UIScrollView with iOS Auto Layout Constraints: Wrong size for subviews -


i'm trying generate view in code. here's hierachy of view object

  • uiscrollview
    • uiview
      • uibutton

the scrollview should same size window. button should big possible. i'm using ios auto layout, constraint strings of objects this

h:|[object]| v:|[object]| 

i've set translatesautoresizingmaskintoconstraints no each object.

the problem button gets default button-size. parent view object (uiview) gets size subviews need.

enter image description here

red: uiscrollview / yellow: uiview

how can force views big scrollview?

when use uiview instead of th uiscrollview works great...

here's code:

    - (void) viewdidload {          [super viewdidload];          // scroll view         uiscrollview* scrollview = [uiscrollview new];         scrollview.backgroundcolor=[uicolor redcolor];         scrollview.translatesautoresizingmaskintoconstraints = no;          //container view         uiview *containerview = [uiview new];         containerview.translatesautoresizingmaskintoconstraints = no;         containerview.backgroundcolor = [uicolor yellowcolor];         [scrollview addsubview:containerview];          // constraints scroll view - container view         [scrollview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:|[containerview]|"                                                  options:0 metrics:nil                                                    views:@{@"containerview":containerview}]];         [scrollview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"v:|[containerview]|"                                                  options:0 metrics:nil                                                    views:@{@"containerview":containerview}]];          // button         uibutton* button = [uibutton buttonwithtype:uibuttontyperoundedrect];         button.translatesautoresizingmaskintoconstraints = no;         [button settitle:@"i'm way small" forstate:uicontrolstatenormal];         [containerview addsubview:button];          // constraints container view - button         [containerview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"h:|[button]|"                                                  options:0 metrics:nil                                                    views:@{@"button":button}]];         [containerview addconstraints:          [nslayoutconstraint constraintswithvisualformat:@"v:|[button]|"                                                  options:0 metrics:nil                                                    views:@{@"button":button}]];         self.view = scrollview;      } 

update: don't know, why happening. if set view in ib, connect outlets , instanciate view in code, scrollview behaves normal view (which bounces vertically). contentsize not calculated correctly. more here. how correctly?

a couple of observations:

  1. constraints subviews in scroll views don't work constraints in other views. they're used set contentsize of scroll view. (see tn2154.) way, throw bunch of stuff on scroll view, set constraints stuff inside it, , contentsize calculated you. it's cool feature, it's antithetical you're trying here.

  2. worse, buttons will, unless set explicit constraint width , height of button, resize according content.

the net effect of these 2 observations existing constraints "(a) set container size of button; (b) let button resize dynamically size of text; , (c) set scrollview's contentsize according size of container (which size of button)."

i'm unclear business problem is. here constraints achieve think technical question was:

- (void)viewdidload {     [super viewdidload];      uiview *view = self.view;      uiscrollview *scrollview = [[uiscrollview alloc] init];     scrollview.backgroundcolor = [uicolor redcolor]; // can see     scrollview.translatesautoresizingmaskintoconstraints = no;     [self.view addsubview:scrollview];      uiview *containerview = [[uiview alloc] init];     containerview.backgroundcolor = [uicolor yellowcolor]; // can see     containerview.translatesautoresizingmaskintoconstraints = no;     [scrollview addsubview:containerview];      uibutton *button = [uibutton buttonwithtype:uibuttontyperoundedrect];     button.translatesautoresizingmaskintoconstraints = no;     [button settitle:@"i'm right size" forstate:uicontrolstatenormal];     [containerview addsubview:button];      nsdictionary *views = nsdictionaryofvariablebindings(scrollview, button, view, containerview);      // set scrollview size of root view      [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[scrollview]|"                                                                       options:0                                                                       metrics:nil                                                                         views:views]];      [self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[scrollview]|"                                                                       options:0                                                                       metrics:nil                                                                         views:views]];      // set container size of main view, , simultaneously     // set scrollview's contentsize match size of container      [view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[containerview(==view)]|"                                                                        options:0                                                                        metrics:nil                                                                          views:views]];      [view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[containerview(==view)]|"                                                                        options:0                                                                        metrics:nil                                                                          views:views]];      // set button size size of container view      [containerview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[button(==containerview)]"                                                                           options:0                                                                           metrics:nil                                                                             views:views]];      [containerview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[button(==containerview)]"                                                                           options:0                                                                           metrics:nil                                                                             views:views]];  } 

frankly, don't understand business intent of ui, feels contortion of auto layout achieve ui. don't know why have scroll view if have "screen sized" content in (unless paging through buttons). don't know why you'd have content view single item in it. don't understand why you're using full-screen button (i'd put tap gesture on root view @ point , call day).

i'll assume have reasons of this, might make sense up, ask desired user experience is, , approach problem fresh see if there's more efficient way achieve desired effect.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -