2017-10-10 4 views
0

UIView를 작성하기 위해 UICollectionView의 앵커 제약 조건을 사용해야합니다. 내가 삽입 한 앵커가 모두 정확하지만 widthAncor에 문제가 ... 나는 설명 :Ancor Constraint에서 너비를 UICollectionView 너비로 설정하십시오.

이 내 CollectionView

_monthCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:monthCollectionLayout]; 
    [self addSubview:_monthCollection]; 

    [_monthCollection.topAnchor constraintEqualToAnchor:self.topAnchor constant:65].active = YES; 
    [_monthCollection.leftAnchor constraintEqualToAnchor:self.leftAnchor constant:2].active = YES; 
    [_monthCollection.rightAnchor constraintEqualToAnchor:self.rightAnchor constant:0].active = YES; 
    [_monthCollection.bottomAnchor constraintEqualToAnchor:self.bottomAnchor constant:0].active = YES; 

이 내있는 UIView

UIView *cursor = [[UIView alloc] init]; 
    cursor.backgroundColor = [UIColor colorWithHexString:@"#E66163" setAlpha:.4]; 
    cursor.layer.borderColor = [UIColor colorWithHexString:@"#E66163" setAlpha:1].CGColor; 
    cursor.layer.borderWidth = 1; 
    cursor.layer.cornerRadius = 15; 
    cursor.translatesAutoresizingMaskIntoConstraints = NO; 
    [_monthCollection addSubview:cursor]; 

    [cursor.topAnchor constraintEqualToAnchor:_monthCollection.topAnchor constant:0].active = YES; 
    [cursor.leftAnchor constraintEqualToAnchor:_monthCollection.leftAnchor constant:0].active = YES; 
    [cursor.widthAnchor constraintEqualToConstant:_monthCollection.frame.size.width].active = YES; 
    [cursor.bottomAnchor constraintEqualToAnchor:_monthCollection.bottomAnchor constant:0].active = YES; 
을 추가입니다

내 UIVIEW의 너비가 _monthCollection.frame.size.width/3과 같아야합니다.

모든 방법을 시도했지만 제약이있는 것 같습니다. 아무런 상관이 없습니다. 결과가 없습니다.

그래서 궁금합니다. 앵커 제약?

답변

1

제약 조건을 만드는 데 잘못된 방법을 사용하고 있습니다. 상수로 설정하는 대신보기의 너비를 컬렉션의 너비로 제한해야합니다. 내가 ObjC를 "말"하지 않습니다,하지만 난 당신이 쉽게 objC 내 스위프트 코드를 다시 작성할 수 있습니다 믿습니다 .. 내가 쉽게 제약 조건 실수 폭이 올바른지 지금

cursor.widthAnchor.constraint(equalTo: _monthCollection.widthAnchor, multiplier: 0.33).isActive = true 
+0

을 ... 당신이 경우 말해 줄 수 다른 제약 조건도 정확합니까? – kAiN

+0

실제로 무엇을 하려는지는 모르지만 collectionView의 너비와 너비가 1/3 인 "커서"가 있고 collectionView의 왼쪽에 위치하는 것이 목표 인 경우 right –

+0

그래서 didSelectItemAtIndexPath에서 leftAncor를 어떻게 애니 메이팅해야합니까? – kAiN