2014-10-04 2 views
0

내 애플에서 무슨 일이 일어나는지 보여주기 위해 작은 데모를 만들었습니다. 기본적으로 화면 너비의 절반을 차지하는 왼쪽 상단의 UIView에 애니메이션을 적용하여 전체 너비를 차지하고 UIView를 화면 오른쪽 상단에 밀어 넣고 싶습니다. 나중에 애니메이션에서 UIView를 다시 화면으로 가져 와서 왼쪽의 뷰를 화면 너비의 절반으로 다시 크기를 조정하려고합니다.Autolayout을 사용하여 UIView의 너비를 변경하십시오.

나는 지금 가지고있는 방식대로 어떻게되는지를 보여주기 위해 몇 가지 스크린 샷을 찍었습니다. 이 밖으로 시작하는 방법 입니다 : This is how it starts out on launch 여기에 다시 This is after trying to bring it back to half size 를 가져하려고 그리고 후 왼쪽보기 This is after enlarging the left UIView 크기를 조정 한 후 뷰의 크기를 변경하는 코드는

- (void)replaceWidthConstraintOnView:(UIView *)view withConstant:(float)constant { 

    [self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { 
     if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeWidth)) { 
      constraint.constant = constant; 
     } 
    }]; 
} 

- (IBAction)changeViewSize { 


    CGFloat blueViewWidth = self.blueView.bounds.size.width; 

    if (blueViewWidth == self.view.bounds.size.width) { 

     blueViewWidth = blueViewWidth/2; 
     [self replaceWidthConstraintOnView:self.blueView withConstant:blueViewWidth]; 

    } 
    else { 
     blueViewWidth = blueViewWidth * 2; 
     [self replaceWidthConstraintOnView:self.blueView withConstant:blueViewWidth]; 
    } 
} 

내가 왜 파란색보기가 화면의 절반 만 덮기 위해 돌아 오지 않는지 궁금합니다. 어떤 도움을 주셔서 감사합니다!

+0

당신은 다음을 시도해 볼 수도 있습니다 : 이것은, 예를 들어, 블루 뷰가 1.0으로 제한 NSLayoutAttributeWidth 승수를 설정하여 부모를 채울 것 constraint.constant = 일정한하여 새 폭을 설정 한 후, 삽입 [containerView layoutIfNeeded]; 여기서 containerView는 blueView의 수퍼 뷰입니다. –

답변

1

필자가 작성한 이유가 확실치 않지만 플로트 크기 대신 일부 관련 제약 조건을 사용할 수 있습니다.

우선, 파란색보기에서 translatesAutoresizingMaskIntoConstraints 속성을 NO로 설정해야합니다.

뭔가 같은 파란색보기를 기본보기의 절반 크기 만들 것입니다 : 모든 제약 조건을 통해 당신은 루프 할 수있는 전체보기를 채우고 승수를 수정하면

[self.view addConstraint:[NSLayoutConstraint 
        constraintWithItem:self.blueView 
        attribute:NSLayoutAttributeWidth 
        relatedBy:NSLayoutRelationEqual 
        toItem:self.view 
        attribute:NSLayoutAttributeWidth 
        multiplier:0.5f 
        constant: 0.0]]; 

만들려면을/상수 당신이 원하는대로.

for(NSLayoutConstraint *constraint in self.view.constraints) 
{ 
    if(constraint.firstAttribute == NSLayoutAttributeWidth && constraint.secondAttribute == NSLayoutAttributeWidth && 
     constraint.firstItem == self.blueView && constraint.secondItem == self.blueView) 
    { 
     constraint.multiplier = 1.0; 
    } 
} 
+0

이것은 금입니다. 고맙습니다! – Idan

관련 문제