2015-01-17 4 views
3

tutorial에 기반하여 뒤집힌 카드보기를 만들려고합니다. UIView를 만들고 카드보기를 추가합니다.Autolayout을 사용하여 SuperView에 CustomView를 맞추기

이 들어 내가 사용자 지정 UIView (Xib 함께) 만든 및 지금까지 잘 작동합니다. addSubview가 호출 된 뷰에 대한 Storyboard에 올바른 제약 조건을 추가했습니다. 이것은 지금까지 작동하지만 사용자 정의 UIView를 추가하면 xib의 크기가 아니라 superview의 크기를 참조합니다.

서브 뷰를 수퍼 뷰에 맞추기 위해 필요한 자동 레이아웃 제약 조건을 어떻게 추가 할 수 있습니까?

감사합니다.

P. 나는 Objective-C로 작성했지만 답이 신속하거나 Objective-C 인 경우 나에게 중요하지 않습니다.

답변

2

잘 모르겠습니다. 무엇이 잘못 되었습니까? 나는 같은 방식으로 다음과 같은 작업을 수행했다 :

1) ProductItemView, UIView의 하위 클래스이며 UIView 객체를 가진 ProductItemView.xib를 생성했다. ProductItemView에 .xib 설정 파일의 소유자 클래스에서

2) .xib 및 기타 파단의 UIView의 개체가 함께 IBOutlet로드와 연계 될 수 있도록. (초기화에서)

enter image description here

3) 이미지를 볼 수 방법 (initWithFrame : 내 경우) 방법은 코드

NSArray *nibViewArray = [[NSBundle mainBundle] loadNibNamed:@"ProductItemView" owner:self options:nil]; 

    if (nibViewArray.count) { 
     UIView *nibView = [nibViewArray objectAtIndex:0]; 
     [self addSubview:nibView]; 

     nibView.translatesAutoresizingMaskIntoConstraints = NO; 

     NSDictionary *viewDict = NSDictionaryOfVariableBindings(nibView); 
     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[nibView]-0-|" options:0 metrics:nil views:viewDict]]; 

     [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[nibView]-0-|" options:0 metrics:nil views:viewDict]]; 

    } 

아래에 넣고 마지막으로 ProdutItemView의 프레임으로 객체를 만들거나 제약 조건을 설정하고 슈퍼 볼에 추가합니다. 제약 조건을 설정하는 경우 translatesAutoresizingMaskIntoConstraints 속성을 NO로 설정하는 것을 잊지 마십시오. 희망이 있으면 도움이 될 것입니다.

+0

조금 더 명확하기 때문에이 대답을 받아 들일 것입니다. 플립 애니메이션의 경우, FlipView가 뒤집힌 "StoryboardView"에 추가 된 후 모든 FlipView (앞면과 뒷면)에 이러한 제약 조건을 추가해야했습니다. 게다가 나는 모든 플립과 함께 이러한 제약 조건을 제거하고 추가해야했습니다 (예 : show front : front Constraints 추가, Constraints 제거). 마지막으로 Storyboard View와 CustomView에 이러한 제약 조건을 추가해야했습니다. – Jan

0

코드에서 원하는 경우. 나를 위해 더 잘 작동하는 것 같고 더 의미가 있습니다.

// make sure myCustomView is added to the superview 
// and ignore the layout guides if the superview is not your view controller 
[myCustomView setTranslatesAutoresizingMaskIntoConstraints: NO]; 

id topGuide = self.topLayoutGuide; 
id bottomGuide = self.bottomLayoutGuide; 

NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(myCustomView, topGuide, bottomGuide); 

[mySuperiew addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myCustomView]|" options:0 metrics: 0 views:viewsDictionary]]; 

[mySuperiew addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topGuide][myCustomView][bottomGuide]|" options:0 metrics: 0 views:viewsDictionary]]; 
+0

부분적으로 작동하는 것 같습니다. 나는 그것을 "스토리 보드보기"에 추가하여 사용자 정의보기에 맞춰야했습니다. 또한 contentview에 맞게 customview에 추가해야했습니다. 그렇게하면 첫 번째 시간은 제대로 작동하지만 두 번 뒤집 으면 (첫 번째보기까지) customview.contentview의 내용이 왜곡됩니다. – Jan

관련 문제