2017-07-27 3 views
-1

J'ai deux images - self.profileImageView, self.profileImageViewOfLoggedInUser qui est 20px l'une de l'autre et je veux les centrer à la vue.images à une vue Centrage - Objectif C Autolayout

Ci-dessous est mon code

static CGFloat const MeetingDetailNameLabelMarginX = 20; 
NSDictionary *views = @{ 
         @"imageView": self.profileImageView, 
         @"imageViewForLoggedInUser": self.profileImageViewOfLoggedInUser, 
         @"nameLabel": self.nameLabel, 
         @"companyNameLabel": self.companyNameLabel, 
         @"positionLabel": self.positionLabel, 
         @"statusLabel": self.statusLabel, 
         }; 

NSDictionary *metrics = @{ 
          @"imagePaddingLeft": @(MeetingDetailImageViewMarginX), 
          @"imagePaddingTop": @(MeetingDetailImageViewMarginY), 
          @"nameLabelPaddingLeft": @(MeetingDetailNameLabelMarginX), 
          @"nameLabelPaddingRight": @(MeetingDetailNameLabelMarginRightX), 
          @"nameLabelPaddingTop": @(MeetingDetailImageViewSize + MeetingDetailImageViewMarginY), 
          @"imageSize": @(MeetingDetailImageViewSize), 
          @"nameLabelHeight": @(nameLabelFrame.size.height), 
          @"otherLabelHeight": @(MeetingDetailOtherLabelHeight), 
          @"dateLabelWidth": @(self.dateLabel.frame.size.width), 
          @"statusLabelWidth": @(statusFrame.size.width), 
          @"statusLabelMarginLeftFromView": @(MeetingDetailImageViewMarginX), 
          }; 

// image left and width 
[self.detailContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[imageView(imageSize)]" 
                      options:0 
                      metrics:metrics 
                       views:views]]; 
[self.detailContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-imagePaddingLeft-[imageViewForLoggedInUser(imageSize)]" 
                      options:0 
                      metrics:metrics 
                       views:views]]; 

// image top and height 
[self.detailContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-imagePaddingTop-[imageView(imageSize)]" 
                      options:0 
                      metrics:metrics 
                       views:views]]; 
[self.detailContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-imagePaddingTop-[imageViewForLoggedInUser(imageSize)]" 
                      options:0 
                      metrics:metrics 
                       views:views]]; 

S'il vous plaît laissez-moi savoir le code à ajouter.

Voici la capture d'écran sur l'ajout du code: -

NSLayoutConstraint *centerXConstraint = [self.detailContainer.centerXAnchor constraintEqualToAnchor:_profileImageView.centerXAnchor]; 
[self.detailContainer addConstraint:centerXConstraint]; 

enter image description here

Répondre

0

Si vous voulez centrer ImageView relative, il est superview, vous ne devriez pas spécifier des marges étranges ou autre chose. Tout ce que vous avez à faire est de spécifier 2 contraintes pour le centrage et 2 contraintes pour la taille de imageView.

De 9 iOS, vous pouvez utiliser ces api simple:

NSLayoutConstraint * centerXConstraint = [superView.centerXAnchor constraintEqualToAnchor: imageView.centerXAnchor]; NSLayoutConstraint * centerYConstraint = [superView.centerYAnchor constraintEqualToAnchor: imageView.centerYAnchor];

NSLayoutConstraint * imageViewHeight = [imageView.heightAnchor contrainteEqualToConstant: heightValue]; NSLayoutConstraint * imageViewWidth = [imageView.widthAnchor constraintEqualToConstant: largeurValeur]; Puis, vous pouvez contraindre la deuxième vue de l'image par rapport à la première vue de l'image (puisque la première est déjà contrainte). Vous pouvez utiliser VFL pour cela, ou peu importe.

+0

Merci @Eugene, j'ai essayé le code ci-dessus et il centre la première image mais pas la deuxième image. Donc, je veux faire une proportion de deux images WRT la vue. S'il vous plaît aider –