Ajout de vues créées par programmation dans scrollview verticalement (disposition linéaire dans iOS)

Je veux append des UIViews créés par programme dans scrollView avec des contraintes de disposition automatique. Comme disposition linéaire verticale dans Android. (Dans l'objective c pas rapide)

J'ai scrollview à l'intérieur du controller de vue dans le storyboard. Donc, fondamentalement, je veux créer et append plusieurs vues dans la disposition verticale sans espaces dans ce scrollview. Et je veux placer la taille de récipient de la vue de défilement dynamicment selon les tailles de vue.

Chaque vue possède une label à l'intérieur et chaque vue doit définir sa hauteur dynamicment en fonction de la taille du text. Mais probablement je dois y revenir plus tard.

for (int i=0; i<10; i++) { UIView *viewOne = UIView.new; [viewOne setTranslatesAutoresizingMaskIntoConstraints:NO]; viewOne.backgroundColor = [UIColor redColor]; NSDictionary *viewsDictionary = @{@"viewOne" : viewOne}; NSDictionary *mesortingcsDictionary = @{@"horizontalSpacing" : @10}; [self.scrollview addSubview:viewOne]; NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-horizontalSpacing-[viewOne]-horizontalSpacing-|" options:NSLayoutFormatDirectionLeadingToTrailing mesortingcs:mesortingcsDictionary views:viewsDictionary]; NSArray *const_Height = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[viewOne(50)]" options:0 mesortingcs:nil views:viewsDictionary]; [viewOne addConstraints:const_Height]; [self.scrollview addConstraints:horizontalConstraints]; } 

Avec ce code, je peux append des vues mais j'ai besoin d'en append une sous l'autre.

Dans le cas d'utilisation de AutoLayout dans le context d'un UIScrollView, je reorderais d'utiliser un initié ContentView sur votre UIScrollView. Ajoutez-les simplement à ViewControllers View dans la fonction viewDidLoad .

 @interface YourViewController () @property (nonatomic, strong) UIScrollView *dataScrollView; @property (nonatomic, strong) UIView* contentView; @end @implementation YourViewController @synthesize dataScrollView, contentView; - (void) viewDidLoad { [super viewDidLoad]; dataScrollView = [[UIScrollView alloc] init]; contentView = [[UIView alloc] init]; // adding the Views programmatically to the hierarchy [self.view addSubview:dataScrollView]; [dataScrollView addSubview:contentView]; // don't translate the AutoresizingMask into constraints dataScrollView.translatesAutoresizingMaskIntoConstraints = NO; contentView.translatesAutoresizingMaskIntoConstraints = NO; // backgroundColor as you wish? dataScrollView.backgroundColor = [UIColor clearColor]; contentView.backgroundColor = [UIColor clearColor]; [dataScrollView setScrollEnabled:YES]; [dataScrollView setAlwaysBounceVertical:YES]; NSDictionary* viewsDictionary = NSDictionaryOfVariableBindings(dataScrollView, contentView); [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[dataScrollView]|" options:0 mesortingcs: 0 views:viewsDictionary]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[dataScrollView]|" options:0 mesortingcs: 0 views:viewsDictionary]]; [dataScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView(==dataScrollView)]|" options:0 mesortingcs: 0 views:viewsDictionary]]; [dataScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 mesortingcs: 0 views:viewsDictionary]]; // see below // [self setUpViews]; } 

Ce code fera le tour pour une seule vue. Ajoutez vos vues requirejses en tant que sous-vue à contentView et définissez les contraintes.

 - (void) setUpViews { UILabel* testLabel = [[UILabel alloc] init]; [testLabel setText:@"Lorem Ipsum"]; testLabel.translatesAutoresizingMaskIntoConstraints = NO; [contentView addSubview: testLabel]; // clean up your code with this mesortingcs Dictionary NSDictionary *mesortingcs = @{@"margintop": @40, @"marginleft": @10, @"marginright": @10, @"marginbottom": @20} // the Views we want to layout with Constraints NSDictionary *viewsDictionary = @{ @"contentView":contentView, @"dataScrollView":dataScrollView, @"testLabel": testLabel} // Horizontal (testlabel) [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-marginleft-[testLabel]-marginright-|" options:0 mesortingcs: mesortingcs views:viewsDictionary]]; // Vertical [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-margintop-[testLabel]-marginbottom-|" options:0 mesortingcs: mesortingcs views:viewsDictionary]]; } 

En vous référant à votre question d'append plusieurs vues dans une boucle, il existe de nombreuses possibilités. Cela pourrait être la solution la plus simple avec constraintsWithVisualFormat .

 - (void) setUpViews { NSDictionary *mesortingcs = @{@"margintop": @40, @"marginleft": @10, @"marginright": @10, @"marginbottom": @20, }; // Alsways like to have contentView and DataScrollView here NSMutableDictionary* dictViews = [[NSMutableDictionary alloc] initWithDictionary:@{@"contentView":contentView, @"dataScrollView":dataScrollView}]; // Basic Leading-Ssortingng for Vertical Constraints NSSsortingng* verticalConstraintsSsortingng = @"V:|-margintop-"; for (NSUInteger index = 0; index < 10; index++) { // Do your Magic here & add your View UILabel* testLabel = [[UILabel alloc] init]; [testLabel setText:@"Lorem Ipsum"]; testLabel.translatesAutoresizingMaskIntoConstraints = NO; [contentView addSubview: testLabel]; // Add to global Mutable Views-Dictionary dictViews [dictViews setObject:testLabel forKey:[NSSsortingng ssortingngWithFormat:@"testLabel%lu", (unsigned long)index]]; // add "[testlabel1]" to the vertical Constraints verticalConstraintsSsortingng = [NSSsortingng ssortingngWithFormat:@"%@[testLabel%lu]-", verticalConstraintsSsortingng, (unsigned long)index]; // Add Horizontal Constraints [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSSsortingng ssortingngWithFormat:@"H:|-marginleft-[testLabel%lu]-marginright-|", (unsigned long)index] options:0 mesortingcs: mesortingcs views:@{@"testLabel-%lu":testLabel}]]; } // Trailing-Ssortingng verticalConstraintsSsortingng = [NSSsortingng ssortingngWithFormat:@"%@marginbottom-|", verticalConstraintsSsortingng]; NSDictionary *viewsDictionary = [[NSDictionary alloc] initWithDictionary:dictViews]; // finally adding the vertical Constraints [contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalConstraintsSsortingng options:0 mesortingcs: mesortingcs views:viewsDictionary]]; } 

J'espère que cela vous aidera à get vos points de vue.