2015-01-20 6 views
0

테이블 행의 이미지를 가운데에 정렬하는 데 문제가 있습니다. 모든 iOS 기기에서 이미지를 일관성있게 배치하고 싶습니다. 그래서 기본 이미지 뷰가있는 사용자 정의 셀을 만들었습니다.셀 이미지 레이아웃 제약

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
     { 
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
      if (self) { 
       // Initialization code 

       float imgWidth = 250; 

       float imgHeight = 250; 
       self.showImage = [[UIImageView alloc]initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/2)-(imgWidth/2),0,imgWidth,imgHeight)]; 
    [self.contentView addSubview:self.showImage]; 
    } 
    return self; 
} 

그래서 인덱스 셀에서 행의 셀에는 다음과 같은 제약 조건이 있습니다.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SponsorCell * cell = [tableView dequeueReusableCellWithIdentifier:@"sponsorCell"]; 

     if (!cell) { 
      cell = [[SponsorCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sponsorCell"]; 
     } 


///....Arithmetic for processing image and other data... 

     image = [UIImage imageWithContentsOfFile:pdfPath]; 

        [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:cell.showImage attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]; 
      [cell.showImage setImage:image]; 

      cell.showImage.accessibilityLabel =accessibilityLabel; 

    } 

내가 코드를 실행 , 나는 다음과 같은 예외가 얻을

: 나는 더 높은 수준으로 제약 조건을 추가 할 필요가 있음을 이해하고

7:26:57.789 Demo[56438:21023305] Exception: Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal 

흐름 포스트를 통해 다른 스택에서을 테이블 뷰와 같은 내 이해가 맞습니까? xib에 제약 조건을 추가 할 수 있습니다. 프로그램을 작성하는 데 막 힘들었습니다. 누군가 내 접근/사고에있어 잘못된 점을 설명해 주시겠습니까? 이것으로 정말 배우고 싶습니다. 잘만되면 나는 분명하다. 예를 들어 주시면 감사하겠습니다.

+1

constraintWithItem : cell을 constraintWithItem : cell.contentView로 변경하려고 할 수 있습니다. 작동하지 않는 경우 : 사용자 정의 셀에 대해 xib를 만들었습니까? 아니면 단지 코드인가? 제약 조건을 추가하기 전에 imageView가 하위보기로 추가되어야합니다. to contentView – Aris

+0

그게 오류를 제거있어! 그러나 여전히 중심에 있지 않습니다. 어떤 아이디어? :) – imnilly

+0

셀 전용 코드. 아니야. 내가 가진 유일한 xib는 tableview입니다. – imnilly

답변

0

와우 .. 나는 그것을 알아낼 수 있었다. 다음은 내가 한 일입니다.

customImageView = [[UIImageView alloc] init]; 
      customImageView.translatesAutoresizingMaskIntoConstraints = NO; 

      [cell.contentView addSubview:customImageView]; 


      image = [UIImage imageWithContentsOfFile:pdfPath]; 



      [customImageView setImage:image]; 

      [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:customImageView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]]; 

      [cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell.contentView attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:customImageView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]]; 

아름답게 작동합니다. 주의 할 점은 autoResizingMaskingIntoConstraints가 NO로 설정되었는지 확인하십시오.

관련 문제