2015-01-09 2 views
5

UITableView 대리자 메서드 viewForHeaderInSection을 사용하여 내 UITableView에 섹션 헤더를 제공하고 있습니다.viewForHeaderInSection autolayout - 핀 너비

나는 처음에이 같은 뷰를 생성 :

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)]; 

그런 다음 자동 레이아웃 그때 내가 특별히 headerView 크기를 지정하지 않으입니다 가지고있는 headerView

문제를 반환하여 일부 하위 뷰를 추가 할 수 있습니다. Autolayout을 사용하여 왼쪽 가장자리에 & 오른쪽 가장자리를보기 너비에 고정합니다. 여기에 문제가 있습니다. Autolayout 코드에서 사용할 수퍼 뷰가 없습니다.

위 코드를 사용하면 헤더보기가 순환 게재시 자동 크기 조정되지 않음을 의미합니다. 회전 후 테이블 뷰를 다시로드해야합니다.

어떻게 생각합니까? headerView은 테이블 뷰의 가장자리에 고정시킬 수 있습니까?

감사

내 테스트에서

답변

2

,이 대답 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; 
} 
+0

감사합니다. 제로 틱 뷰를 반환하면 올바른 것을 사용했습니다. 크기 때문에 tableview.bounds를 사용할 필요가 없지만 회전 할 때이 값은 올바르지 않습니다. 당신은 tableview를 다시로드해야합니다. 나는 가장자리를 고정시킬 슈퍼 뷰가 없다고 추측하고있다. – Darren

+0

그래, 자동 레이아웃의 "경계선 밖"으로 취급되는 것 같습니다. 과거에 이런 동작이 필요할 때 실제로'UITableViewCell'을 서브 클래 싱하여 섹션 헤더 대신 사용했습니다. 실제로 섹션을 사용하는 경우 행 0의 셀일 수 있습니다. 그렇게하면 ** ** 회전 지원을받을 수 있습니다. – mbm29414

+0

사실 그건 나쁜 생각이 아닙니다. 사실 그것은 테이블 스크롤시 화면에 머물러있는 섹션 헤더도 멈추게 할 것입니다. – Darren

관련 문제