2016-08-02 1 views
0

Je suis en train d'ajouter des contraintes à UIScrollView avec une étiquette (sous-vue scrollview) mais le scrollview ne défiler et arrière ne marcherait jamais.Ajouter à Constraint ScrollView

float y = self.navigationController.navigationBar.frame.size.height + 30; 
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, 0,0)]; 
[self.scrollView setBackgroundColor:[UIColor blueColor]]; 
[self.scrollView setScrollEnabled:YES]; 
[transparentImageView addSubview:self.scrollView]; 


self.scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-120.0]]; 
//leading 
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:20.0f]]; 
//trailing 
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-20.0]]; 
[transparentImageView addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:transparentImageView attribute:NSLayoutAttributeTop multiplier:1.0f constant:y]]; 

self.shineText = [[RQShineLabel alloc]initWithFrame:CGRectMake(0,0, 0, 0)]; 
[self setupText]; 
[self.shineText setBackgroundColor:[UIColor redColor]]; 
[self.scrollView addSubview:self.shineText]; 


self.shineText.translatesAutoresizingMaskIntoConstraints = NO; 
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height); 

//bottom 
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-10.0f]]; 
//leading 
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]]; 
//trailing 
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-500.0f]]; 
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:self.shineText attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:20]]; 

Répondre

0

Si vous avez cette ligne self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height); alors votre point de vue de défilement jamais faire défiler du tout parce que la taille de votre contenu est le même que le cadre de défilement, la taille du contenu de votre scrollView doit être supérieure à celle du cadre ScrollView pour faites défiler

Essayez quelque chose comme ça

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, 1000); 

alors votre ScrollView doit faire défiler

J'espère que cela vous aide

+0

merci, je l'ai essayé la contrainte de fuite pour le shinetext ne fonctionne pas. – user1688346

+0

essayez ceci 'self.scrollView.contentSize = CGSizeMake (self.scrollView.frame.size.width, self.shineText.frame.size.height); ' –

0

En général, tout en travaillant avec UIScrollView et mises en page automatiques, nous devons ajouter un UIView comme vue enfant sur la vue de défilement avec la même taille que défilante.


Here is tutorial qui explique cela en détails.
Mais ce tutoriel est pour l'utilisation de l'affichage déroulant dans le fichier .nib.

Pour ajouter la vue de défilement par programme, passez par Apple developer technical note.
Il suggère que:

Utilisez des contraintes pour mettre les sous-vues dans la vue de défilement, en étant sûr que les contraintes lient aux quatre bords de la vue de défilement et ne reposent pas sur le point de vue de défilement pour obtenir leur taille .

0

Essayez cette façon délicate de travailler avec scrollview dans le storyboard. J'ai créé un petit didacticiel vidéo pour montrer à quel point il est facile de travailler avec scroll view. Vous devriez avoir une petite connaissance de autolayout.

step 1: change your viewcontroller size as your need

step 2: add a scroll view and setup constarints (top, lead, trail and top) to the conatainer view.

step 3: then add another view on to scroll view(this is your child view where you add your all other IBOutlets)

step 4: then add constraints to it. (top, lead, trail and top to scroll view) + (width and height to it self)

step 5: create an outlet to the child view width to your controller and give the width programatically(the device width)

ce petit tutoriel vous montre comment le faire ..

part one : less thant 5 min

part two : less than 5 min

espère que cela vous aidera à vous.