내가 만드는 프레임 워크의 목적에 따라 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>"
)
귀하의 편집에 대한 응답으로 : 귀하의 오류가 암시 하듯이, NSAutoresizingMaskLayoutConstraints가 수동으로 생성하는 제한 조건과 충돌 할 수 있습니다. 이 문제를 방지하려면 self.view의 translatesAutoresizingMaskIntoConstraints 속성을 NO로 설정해야합니다. 편집 : 방금 두 번째 편집 및이 주석 올바르지 않다 보았다. – Alex
사이드 노트 : heightConstraint에 대해서는 두 번째 속성을 NSLayoutAttributeNotAnAttribute로 설정해야한다고 생각합니다. – Johan