,이 대답 here에서, UIView
가 자동으로 원점 (0, 0)
로 설정하고, 그 메소드에서 반환, 그 높이가 너비로 설정 -tableView: heightForHeaderInSection:
와 폭에서 반환 된 값으로 설정 UITableView
.
UIView
에 컨트롤을 추가 할 수 있었고 init
메서드에서 특정 크기를 지정하지 않고도 자동 레이아웃으로 레이아웃 할 수있었습니다. 여기
- (void)layoutControls {
[self.headerView addSubview:self.segmentedControl];
[self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[control]-(margin)-|"
options:0
metrics:@{@"margin": @(self.segmentedControlLeftRightMargin)}
views:@{@"control": self.segmentedControl}]];
[self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(margin)-[control(==height)]"
options:0
metrics:@{@"margin": @(self.segmentedControlTopMargin),
@"height": @(self.segmentedControlHeight)}
views:@{@"control": self.segmentedControl}]];
[self.headerView addSubview:self.searchBar];
[self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[control]-(margin)-|"
options:0
metrics:@{@"margin": @(self.searchBarLeftRightMargin)}
views:@{@"control": self.searchBar}]];
[self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[control1]-(margin1)-[control2]-(margin2)-|"
options:0
metrics:@{@"margin1": @(self.segmentedControlBottomMargin),
@"margin2": @(self.searchBarBottomMargin)}
views:@{@"control1": self.segmentedControl,
@"control2": self.searchBar}]];
}
가 UITableViewDelgate
프로토콜에 대한 방법은 다음과 같습니다
가
self.headerView = [[UIView alloc] init];
여기에 내가 헤더보기 내부의 컨트롤을 배치 코드입니다 :
다음은 헤더보기를 만들 내 코드입니다 :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
// The hard-coded values are accurate for my controls, but you might need more advanced logic
return 44.0f + self.segmentedControlBottomMargin + 44.0f + self.searchBarBottomMargin;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return self.headerView;
}
감사합니다. 제로 틱 뷰를 반환하면 올바른 것을 사용했습니다. 크기 때문에 tableview.bounds를 사용할 필요가 없지만 회전 할 때이 값은 올바르지 않습니다. 당신은 tableview를 다시로드해야합니다. 나는 가장자리를 고정시킬 슈퍼 뷰가 없다고 추측하고있다. – Darren
그래, 자동 레이아웃의 "경계선 밖"으로 취급되는 것 같습니다. 과거에 이런 동작이 필요할 때 실제로'UITableViewCell'을 서브 클래 싱하여 섹션 헤더 대신 사용했습니다. 실제로 섹션을 사용하는 경우 행 0의 셀일 수 있습니다. 그렇게하면 ** ** 회전 지원을받을 수 있습니다. – mbm29414
사실 그건 나쁜 생각이 아닙니다. 사실 그것은 테이블 스크롤시 화면에 머물러있는 섹션 헤더도 멈추게 할 것입니다. – Darren