2017-03-01 4 views
0

자동 레이아웃 내보기의 높이를 설정하려면 NSLayoutConstraint 코드로 내보기 및이 같은 이상한 오류가 있습니다 : 324.867 같은보기의 높이를 설정할 때 사용합니다. 오류 Unable to simultaneously satisfy constraints가 발생합니다.분수를 사용할 때 [LayoutConstraints] 오류가 발생합니다. 그러나 그 숫자를 반올림 할 때. 오류가 사라졌습니다

하지만 반올림 한 경우 324.867에서 324입니다. 오류가 사라졌습니다.

왜 그런 일이 일어날 수 있습니까?

내 코드입니다 : 내 topicTitle 높이 (표에 의해 자동으로 내 수퍼 년대의 설정 높이를 원하는 때문에

let estimateSizeOfTopic = CGSize(width: self.frame.width - 10.0, height: 1000.0) 
let attributeOfTopic = [NSFontAttributeName: UIFont.systemFont(ofSize: 14)] 
let estimateHeight = NSString(string: topicTitle.text!).boundingRect(with: estimateSizeOfTopic, options: .usesLineFragmentOrigin, attributes: attributeOfTopic, context: nil).height 

NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: estimateHeight).isActive = true 

NSLayoutConstraint(item: topicTitle, attribute: .top, relatedBy: .equal, toItem: superView, attribute: .top, multiplier: 1.0, constant: 5.0).isActive = true 
NSLayoutConstraint(item: topicTitle, attribute: .left, relatedBy: .equal, toItem: superView, attribute: .left, multiplier: 1.0, constant: 5.0).isActive = true 
NSLayoutConstraint(item: topicTitle, attribute: .right, relatedBy: .equal, toItem: superView, attribute: .right, multiplier: 1.0, constant: -5.0).isActive = true 
NSLayoutConstraint(item: topicTitle, attribute: .bottom, relatedBy: .equal, toItem: superView, attribute: .bottom, multiplier: 1.0, constant: -5.0).isActive = true 

이 // 내 수퍼는 아무 제약이없는 ... 나는 그런 식으로 코딩 셀 자동 크기 조정보기

첫 번째 제약 조건에서 오류가 발생합니다.

//If I use this line of code. It raise an error 
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: estimateHeight).isActive = true 

//If I use this line of code instead. The error is gone 
NSLayoutConstraint(item: topicTitle, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: round(estimateHeight)).isActive = true 

나는 둥근 내 estimateHeight 내가 반올림하지 않은 경우,이 오류 .. 를 제기하지만 오류가의 영감 때문이다. 이러한 오류의 수학 (문자)를 추가하기 때문에

[LayoutConstraints] Unable to simultaneously satisfy constraints. 
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x608000484560 UITextView:0x7f9049080c00'It is a long established ...'.height == 324.867 (active)>", 
"<NSLayoutConstraint:0x608000285280 V:|-(5)-[UITextView:0x7f9049080c00'It is a long established ...'] (active, names: '|':SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier')>", 
"<NSLayoutConstraint:0x60800029fe50 UITextView:0x7f9049080c00'It is a long established ...'.bottom == SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier'.bottom - 5 (active)>", 
"<NSLayoutConstraint:0x60800029fcc0 'UIView-Encapsulated-Layout-Height' SatacusWorld.PostViewCell:0x7f9049096a00'postIdentifier'.height == 334.8 (active)>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000484560 UITextView:0x7f9049080c00'It is a long established ...'.height == 324.867 (active)> 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

답변

0

그 이유가 무엇인지 알아 냈습니다.

: 나는 코드

tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 100.0 

이 두 라인으로 그리고 백그라운드에서 테이블보기 크기 조정 자동을 사용하기 때문에 , UITableViewAutomaticDimension는 자동으로 해당 번호 1 소수점에 자리 예를 들어

라운드 내 셀 높이를 계산합니다 세포 내 전체 높이입니다

경우 324.833

324.8

에 그 숫자를 반올림 것입니다

그래서.셀의 전체 높이 (324.833가) 세포의 높이 (324.8)

그리고 그 오류를 수정 한 후 큰, 난 그냥 UITableViewAutomaticDimension 라운드 전에 내 번호를 반올림이

0

이 확실히 호기심 버그 : 내보기의 모든 일들이 여전히 잘

이 디버그 창 말한 본다.

그러나 놀랍지 않습니다. boundingRect(with:options:attributes:context:)의 예상치를 레이아웃 제약 조건을 사용하여 나중에 자동 레이아웃 계산과 일치시킬 것으로 예상하는 경우 이보다 정확하지 않은 예제로도 실패 할 수 있습니다. 타이포그래피에 대한 자세한 내용과 글꼴이 다른 크기로 렌더링되는 방법에 대한 세부 정보 때문입니다.

이 텍스트보기에서 높이 제한을 설정하는 것을 피할 수 있습니까? 전체 텍스트를 표시하려면 UITextView 속성 scrollEnabledfalse으로 설정해야합니다. 이렇게하면 적절한 시간 (수평 경계가 설정되었을 때)에 자동 레이아웃 엔진에 해당 텍스트의 정확한 크기가 제공됩니다. 자동 셀 크기 조정을 사용하여 셀에 있으면 크기와 셀의 크기를 올바르게 조정해야합니다. 희망이 도움이됩니다.