프로그래밍 방식으로 생성 한 UITableViewController
(xib은 없습니다)입니다. autolayout 제약 조건이있는 xib에서로드 된 UIView
을 사용하려고하는데 내 tableHeaderView
(섹션 헤더 아님)으로 사용하고 있습니다. 그런 다음 동적 텍스트가 포함될 UILabel
의 크기 조정에 따라 UIView
의 자동 크기를 조정해야합니다. 크기 조정 UIView를 xib에서 tableHeaderView로로드했습니다.
이
내가 만드는 방법 내UIView
:
class HeaderTableLargeHeaderView: UIView {
// Our title label with autolayout constraints
@IBOutlet weak var titleLabel: UILabel!
// Convience method to load for xib
class func instanceFromNib() -> UIView {
return UINib(nibName: "HeaderTableLargeHeaderView", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
}
override func layoutSubviews() {
super.layoutSubviews()
titleLabel.preferredMaxLayoutWidth = titleLabel.bounds.width
}
}
가 그럼 난이처럼로드하려고 내
UITableViewController
:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let header = HeaderTableLargeHeaderView.instanceFromNib()
header.frame = CGRectMake(0, 0, self.view.frame.size.width, 100)
let height = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
var frame = header.frame
frame.size.height = height
header.frame = frame
header.setNeedsLayout()
header.layoutIfNeeded()
tableView.tableHeaderView = header
}
그러나 나는 이러한 오류를 얻을 :
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:0x7c061050 V:[UILabel:0x7c0b77e0'Label']-(8)-| (Names: '|':Care_Com.HeaderTableLargeHeaderView:0x7c04a550)>",
"<NSLayoutConstraint:0x7c0c6590 V:|-(8)-[UILabel:0x7c0b77e0'Label'] (Names: '|':Care_Com.HeaderTableLargeHeaderView:0x7c04a550)>",
"<NSLayoutConstraint:0x797576d0 'UIView-Encapsulated-Layout-Height' V:[Care_Com.HeaderTableLargeHeaderView:0x7c04a550(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7c061050 V:[UILabel:0x7c0b77e0'Label']-(8)-| (Names: '|':Care_Com.HeaderTableLargeHeaderView:0x7c04a550)>
나는 이해할 수 있다고 생각하지만 해결 방법을 모르겠습니다. 그것이 내게 말하고있는 것이 그것이 UILabel
의 한계를 잘 모르기 때문에 UIView
... 나는 생각합니다.
이렇게하면 아무런 오류가 발생하지 않고 높이가 정확하게로드되지만 100
과 같이 정적입니다.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let header = HeaderTableLargeHeaderView.instanceFromNib()
header.frame = CGRectMake(0, 0, self.view.frame.size.width, 100)
header.setNeedsLayout()
header.layoutIfNeeded()
tableView.tableHeaderView = header
}
나는 아직도 도움이되지 않는 이러한 설정을 시도했습니다 :(
self.view.translatesAutoresizingMaskIntoConstraints = false
self.tableView.translatesAutoresizingMaskIntoConstraints = false
나는 또한이 일을 시도했다 : 그러나 오류를 제거한다
header.translatesAutoresizingMaskIntoConstraints = false
을 즐길 수 있지만, 당신은 몇 가지 추가 코드를 작성했습니다. 나는 진짜 예제로 해결할 수 있습니다. 단일 컨트롤러 및 이러한 파일을 사용하여 코드를 샘플 프로젝트로 게시하십시오. 나는 그것을 해결할 것이다. –
굉장! 가능한 한 빨리 샘플 프로젝트를 만들려고합니다. – random
@ M.K. 여기 샘플 프로젝트를 만들었습니다 : https://github.com/coryhymel/HeaderTest/tree/master 정말 고마워요 !!! – random