programing

UIView의 고정 높이 구속조건을 프로그래밍 방식으로 업데이트하려면 어떻게 해야 합니까?

css3 2023. 4. 10. 22:09

UIView의 고정 높이 구속조건을 프로그래밍 방식으로 업데이트하려면 어떻게 해야 합니까?

나는 가지고 있다UIViewXcode Interface Builder를 사용하여 제약을 설정합니다.

이제 그것을 업데이트해야 합니다.UIView인스턴스의 높이가 프로그래밍 방식으로 일정합니다.

다음과 같은 기능이 있습니다.myUIView.updateConstraints()사용법을 몰라요.

Interface Builder에서 높이 구속조건을 선택하고 콘센트를 꺼냅니다.따라서 뷰의 높이를 변경하고 싶을 때 아래 코드를 사용할 수 있습니다.

yourHeightConstraintOutlet.constant = someValue
yourView.layoutIfNeeded()

방법updateConstraints()의 인스턴스 메서드입니다.UIView. 구속조건을 프로그래밍 방식으로 설정할 때 유용합니다.뷰에 대한 제약 조건을 업데이트합니다.상세한 것에 대하여는, 여기를 클릭해 주세요.

여러 개의 구속조건이 있는 뷰가 있는 경우 여러 개의 콘센트를 작성하지 않아도 되는 훨씬 쉬운 방법은 다음과 같습니다.

인터페이스 빌더에서 식별자를 변경할 각 제약 조건을 지정합니다.

여기에 이미지 설명 입력

그런 다음 코드에서는 다음과 같이 여러 제약 조건을 수정할 수 있습니다.

for constraint in self.view.constraints {
    if constraint.identifier == "myConstraint" {
       constraint.constant = 50
    }
}
myView.layoutIfNeeded()

여러 개의 구속조건에 동일한 식별자를 지정할 수 있으므로 구속조건을 그룹화하고 동시에 수정할 수 있습니다.

바꾸다HeightConstraint그리고.WidthConstraint작성하지 않고IBOutlet.

참고: 이 확장자를 사용하여 이 구속조건을 가져온 후 Storyboard 또는 XIB 파일에서 높이 또는 너비 구속조건을 할당합니다.

이 확장을 사용하여 높이와 폭을 가져올 수 있습니다. 구속조건:

extension UIView {

var heightConstraint: NSLayoutConstraint? {
    get {
        return constraints.first(where: {
            $0.firstAttribute == .height && $0.relation == .equal
        })
    }
    set { setNeedsLayout() }
}

var widthConstraint: NSLayoutConstraint? {
    get {
        return constraints.first(where: {
            $0.firstAttribute == .width && $0.relation == .equal
        })
    }
    set { setNeedsLayout() }
}

}

다음을 사용할 수 있습니다.

yourView.heightConstraint?.constant = newValue 

제약을 IBOutlet으로 VC에 드래그합니다.그런 다음 관련 값(및 기타 속성)을 변경할 수 있습니다. 설명서를 확인하십시오.

@IBOutlet myConstraint : NSLayoutConstraint!
@IBOutlet myView : UIView!

func updateConstraints() {
    // You should handle UI updates on the main queue, whenever possible
    DispatchQueue.main.async {
        self.myConstraint.constant = 10
        self.myView.layoutIfNeeded()
    }
}

원하는 경우 부드러운 애니메이션으로 제약 조건을 업데이트할 수 있습니다. 아래 코드 청크를 참조하십시오.

heightOrWidthConstraint.constant = 100
UIView.animate(withDuration: animateTime, animations:{
self.view.layoutIfNeeded()
})

먼저 높이 구속조건을 뷰 컨트롤러에 연결하여 아래 코드와 같은 IBOutlet을 생성합니다.

@IBOutlet weak var select_dateHeight: NSLayoutConstraint!

그 후 아래 코드를 표시하여 로드 또는 내부 액션을 표시하였습니다.

self.select_dateHeight.constant = 0 // we can change the height value

버튼 클릭 안에 있는 경우

@IBAction func Feedback_button(_ sender: Any) {
 self.select_dateHeight.constant = 0

}

레이아웃 제약을 갱신하려면 constant 속성과 다음 시간 후에 layoutIfNeeded를 호출하기만 하면 됩니다.

myConstraint.constant = newValue
myView.layoutIfNeeded()

위의 방법이 작동하지 않을 경우 Dispatch.main.async{}블록에서 업데이트하십시오.그러면 layoutIfNeeded() 메서드를 호출할 필요가 없습니다.

Create an IBOutlet of NSLayoutConstraint of yourView and update the constant value accordingly the condition specifies.

//Connect them from Interface 
@IBOutlet viewHeight: NSLayoutConstraint! 
@IBOutlet view: UIView!

private func updateViewHeight(height:Int){
   guard let aView = view, aViewHeight = viewHeight else{
      return
   }
   aViewHeight.constant = height
   aView.layoutIfNeeded()
}

애니메이션으로 구속조건을 갱신하면 UIView.animate에서 레이아웃 서브뷰를 추가합니다.
예를들면,

@IBOutlet viewHeight: NSLayoutConstraint! 
@IBOutlet view: UIView!

private func updateViewHeight(height:Int){
   guard let aView = view, aViewHeight = viewHeight else{
      return
   }
 aViewHeight.constant = height
 UIView.animateWithDuration(0.5, delay: 0) { [self] in
   aView.layoutIfNeeded()
 }
}

언급URL : https://stackoverflow.com/questions/42669554/how-to-update-the-constant-height-constraint-of-a-uiview-programmatically