2017-07-27 15 views
-1

私は、2枚の画像がある - self.profileImageView20ピクセル互いに離間されself.profileImageViewOfLoggedInUserを及びIビューにそれらをセンタリングします。以下はセンタリング画像 - 自動レイアウトのObjective C

は私が追加するコードを教えてください私のコード

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]]; 

です。

は、以下のコードを追加することで、スクリーンショットです: - あなたはそれはスーパーだImageViewの相対を中心にしたい場合は

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

enter image description here

答えて

0

は、あなたはいくつかの奇妙なマージンまたは何か他のものを指定しないでください。 centeringには2つの制約を指定し、imageViewには2つの制約を指定するだけです。

iOSの9からは、これらの単純なAPIを使用することができます。

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

NSLayoutConstraint * imageViewHeight = [imageView.heightAnchor constraintEqualToConstant:heightValue]; NSLayoutConstraint * imageViewWidth = [imageView.widthAnchor constraintEqualToConstant:widthValue];

次に、第1の画像ビューに対して(第1の画像ビューはすでに制約されているので)第2の画像ビューを制限することができます。 VFLを使うことができます。

+0

ありがとう@Eugene、私は上記のコードを試して、それは最初の画像を中心にしますが、2番目の画像を中心にしません。だから、私は2つの画像の割合を表示したい。助けてください –