2013-08-14 2 views
0

내가 만드는 프레임 워크의 목적에 따라 UIView 개체 프레임 속성을 설정해야하지만 AutoLayout이 설정된 동안에는 그 작업을 수행 할 수 없습니다. 제 아이디어는 모든 레이아웃 제약 조건을 UIView 개체에서 제거한 다음 원하는 레이아웃을 기반으로 새로운 레이아웃 제약 조건을 만들 수 있다는 것입니다.런타임시 왼쪽, 위쪽, 너비 및 높이 제한을 변경하는 경우

이것이 내가 가지고있는 것입니다.하지만 오류가 발생하는 것은 아닙니다 (아래). [self.view addConstraint:heightConstraint];[self.view.superview addConstraint:heightConstraint];에 요소를 변경 한 후

편집

표시하지만 콘솔

오류

에 다음과 같은 메시지가 점점 오전 :

//Remove current constraints 
[self.view removeConstraints:self.view.constraints]; 


//create x constraint based on new x value 
NSLayoutConstraint *xConstraint =[NSLayoutConstraint 
            constraintWithItem:self.view 
            attribute:NSLayoutAttributeLeft 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view.superview 
            attribute:NSLayoutAttributeLeft 
            multiplier:1.0 
            constant:self.x]; 
// add new x constrain 
[self.view.superview addConstraint:xConstraint]; 

//create y constraint based on new y value 
NSLayoutConstraint *yConstraint =[NSLayoutConstraint 
            constraintWithItem:self.view 
            attribute:NSLayoutAttributeTop 
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view.superview 
            attribute:NSLayoutAttributeTop 
            multiplier:1.0 
            constant:self.y]; 

[self.view.superview addConstraint:yConstraint]; 

//create width constraint based on new width value 
NSLayoutConstraint *widthConstraint =[NSLayoutConstraint 
            constraintWithItem:self.view 
            attribute:NSLayoutAttributeWidth 
            relatedBy:NSLayoutRelationEqual 
            toItem:nil 
            attribute:NSLayoutAttributeWidth 
            multiplier:1.0 
            constant:self.width]; 

[self.view.superview addConstraint:widthConstraint]; 

//create height constraint based on new height value 
NSLayoutConstraint *heightConstraint =[NSLayoutConstraint 
            constraintWithItem:self.view 
            attribute:NSLayoutAttributeHeight 
            relatedBy:NSLayoutRelationEqual 
            toItem:nil 
            attribute:NSLayoutAttributeHeight 
            multiplier:1.0 
            constant:self.height]; 

[self.view.superview addConstraint:heightConstraint]; 

이 런타임에 발생하는 것입니다

아마도 다음 목록에있는 제약 중 적어도 하나는 원치 않는 것입니다. 이것을 시도하십시오 : (1) 각 제약 조건을보고 당신이 예상하지 못한 것을 알아 내려고 시도하십시오; (2) 원하지 않는 제약 조건 또는 제약 조건을 추가 한 코드를 찾아 수정하십시오.

(
    "<NSLayoutConstraint:0x91475c0 H:|-(1)-[UIButton:0x728ab00](LTR) (Names: '|':UIScrollView:0x72845b0)>", 
    "<NSLayoutConstraint:0x729dad0 H:|-(25)-[UIImageView:0x7284920](LTR) (Names: '|':UIScrollView:0x72845b0)>", 
    "<NSLayoutConstraint:0x72924d0 UIButton:0x728ab00.leading == UIImageView:0x7284920.leading>" 
) 
+0

귀하의 편집에 대한 응답으로 : 귀하의 오류가 암시 하듯이, NSAutoresizingMaskLayoutConstraints가 수동으로 생성하는 제한 조건과 충돌 할 수 있습니다. 이 문제를 방지하려면 self.view의 translatesAutoresizingMaskIntoConstraints 속성을 NO로 설정해야합니다. 편집 : 방금 두 번째 편집 및이 주석 올바르지 않다 보았다. – Alex

+0

사이드 노트 : heightConstraint에 대해서는 두 번째 속성을 NSLayoutAttributeNotAnAttribute로 설정해야한다고 생각합니다. – Johan

답변

0

귀하의 문제는 당신이 self.view하는 제약 조건을 추가하는 것입니다 (참고 : 당신이 이해하지 못하는 NSAutoresizingMaskLayoutConstraints을보고있는 경우, UIView의 속성 translatesAutoresizingMaskIntoConstraints에 대한 설명서를 참조)이 참조의 self.view .superview. 이것은 효과가 없으며 "제약 조건이 뷰의 하위 트리 외부에서 참조를 참조합니까? 불법입니다."

정확히 무엇을하려고합니까? 이 문제에 대한 또 다른 접근법이 있어야합니다. 잠재적으로 수퍼 뷰에 제약 조건을 추가 할 수는 있지만 상황에 대해 완전히 확신 할 수는 없습니다.

+0

버그가 슈퍼 뷰 픽스에 제약 조건을 추가했지만 제약 조건으로 캡슐화 된 새로운 x, y, 너비, 높이 데이터를 기반으로 슈퍼 뷰를 기준으로 뷰를 배치하는 개념을 수정하지 않음 – Cyprian

+0

어떻게 self.view를 그것의 superview 안에 두는가 너는 지금 얻고있는 무엇이 결과 이는가? – Alex

+0

님이 질문을 편집했습니다. superview에 제약 조건을 추가 한 후에 예상했던대로 개체 위치가 표시되지만 콘솔에 추가 정보가 표시되어 올바른 작업을 수행하지 못합니다. 질문에 콘솔 정보를 게시했습니다. – Cyprian

관련 문제