ios - How to do Auto Layout XIB being added programmatically? -
i'm trying add subview programmatically people can horizontally scroll section. looks subview doesn't fit superview , content not showing
here's code viewcontroller.m
- (void)viewdidlayoutsubviews { [super viewdidlayoutsubviews]; (int = 0; < 2; i++) { cgrect frame; frame.origin.x = self.scrollview.frame.size.width * i; frame.size = self.scrollview.frame.size; self.scrollview.pagingenabled = yes; newssection *sectionview = [[newssection alloc] initwithframe:frame]; [self.scrollview addsubview:sectionview]; } self.scrollview.contentsize = cgsizemake(self.scrollview.frame.size.width * 2, self.scrollview.frame.size.height); } this storyboard , constraints.

and newssection.xib 
this code of newssection.m
-(id) initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if(self) { [[nsbundle mainbundle] loadnibnamed:@"newssection" owner:self options:nil]; [self addsubview:self.view]; } return self; } and result i'm seeing

update: product code here https://github.com/noppanit/uiscrollviewautolayout
i'll start off read regarding uiscrollview , autolayout: https://developer.apple.com/library/ios/technotes/tn2154/_index.html
now, here's sample code should keep going. need make newsview managed viewcontroller, e.g. newsviewcontroller, , initialize newsviewcontroller before using in setupnewsview:
- (void)viewdidload { [super viewdidload]; [self setupnewsview]; } - (void) setupnewsview { uiview *newsview = [[newssection alloc] initwithframe:cgrectzero]; self.scrollview.translatesautoresizingmaskintoconstraints = no; newsview.translatesautoresizingmaskintoconstraints = no; [self.scrollview addsubview:newsview]; nsdictionary *viewsdict = @{ @"newsview": newsview }; [self.scrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|[newsview]|" options:nslayoutformatalignalltop | nslayoutformatalignallbottom metrics:nil views:viewsdict]]; [self.scrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[newsview]|" options:0 metrics:nil views:viewsdict]]; [self.scrollview addconstraint:[nslayoutconstraint constraintwithitem:newsview attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:self.scrollview attribute:nslayoutattributewidth multiplier:1.0f constant:0.0f]]; [self.scrollview addconstraint:[nslayoutconstraint constraintwithitem:newsview attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:self.scrollview attribute:nslayoutattributeheight multiplier:1.0f constant:0.0f]]; } edit:
i'm sorry. missed add line in solution before:
[self.scrollview addconstraints:[nslayoutconstraint constraintswithvisualformat:@"v:|[newsview]|" options:0 metrics:nil views:viewsdict]]; regarding code sample: first, fill viewcontroller code sample depicts. incorporated change first sample. remove code overrides viewdidlayoutsubviews.
then, need change initializer of newsview.
now looks this:
-(id) initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if(self) { [[nsbundle mainbundle] loadnibnamed:@"newssection" owner:self options:nil]; [self addsubview:self.view]; } return self; } one problem here load nib don't assign view nib view object. reference (https://developer.apple.com/library/prerelease/ios/documentation/uikit/reference/nsbundle_uikitadditions/index.html) depicts loadnibnamed:owner:options: returns top level objects. since have 1 top level object in our xib need assign ourself. hacky , introduces problem nib leak in future. better make uiviewcontroller xib here.
to make code work, initializer of newsview should this. won't connect outlet.
- (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { self = [[nsbundle mainbundle] loadnibnamed:@"newssection" owner:self options:nil][0]; } return self; }
Comments
Post a Comment