将textLabel垂直放置在tableView的UITableViewHeaderFooterView中:willDisplayHeaderView:forSection:

我正在尝试为tableViewHeader垂直居中文本标签。 这里是相关代码- (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section:

 - (void) tableView: (UITableView*) tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if(![view isKindOfClass:[UITableViewHeaderFooterView class]]){ return; } UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; UILabel* label = tableViewHeaderFooterView.textLabel; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont rev_lightAppFontOfSize: 24.0f]; label.textColor = [UIColor rev_colorWithHex: 0x858585]; label.text = [label.text capitalizedString]; label.center = tableViewHeaderFooterView.center; // label not in superview yet tableViewHeaderFooterView.layer.borderColor = [UIColor greenColor].CGColor; tableViewHeaderFooterView.layer.borderWidth = 2.0f; tableViewHeaderFooterView.contentView.layer.borderColor = [UIColor purpleColor].CGColor; tableViewHeaderFooterView.contentView.layer.borderWidth = 2.0f; label.layer.borderColor = [UIColor orangeColor].CGColor; label.layer.borderWidth = 2.0f; } 

标签对象不是tableViewHeaderFooterView的子视图,因此居中代码似乎不工作。 有什么想法吗?

那么我最终搞清楚如何做到这一点。 不幸的是,你不能在tableView:willDisplayHeader:函数中处理垂直居中,因为tableViewHeaderFooterView在幕后做了一些棘手的tableView。 因此,我发现最好的方法是在您自己的标题视图中返回一个标签。 相关的代码在这里:

 - (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section { UIView* view = [[UIView alloc] init]; UILabel* label = [[UILabel alloc] init]; label.text = [self tableView: tableView titleForHeaderInSection: section]; label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; label.translatesAutoresizingMaskIntoConstraints = NO; [view addSubview:label]; [view addConstraints: @[[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0], [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]]]; return view; } 

希望这会帮助别人!

干杯

我能够将部分标签居中在willDisplayHeaderView中。

 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if([view isKindOfClass:[UITableViewHeaderFooterView class]]){ UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; [tableViewHeaderFooterView.textLabel setTextAlignment:NSTextAlignmentCenter]; } } 
 func configureHeaderView(header: UITableViewHeaderFooterView, forSection section: Int) { switch section { case 0: header.textLabel!.text = "BASES \(selectedBases)/\(menuItem.freeBases!.integerValue)" case 1: header.textLabel!.text = "TOPPINGS \(selectedToppings)/\(menuItem.freeToppings!.integerValue)" case 2: header.textLabel!.text = "PREMIUMS \(selectedPremiums)/\(menuItem.freePremiums!.integerValue)" case 3: header.textLabel!.text = "DRESSINGS \(selectedDressings)/\(menuItem.freeDressings!.integerValue)" default: break } } func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { let headerView = view as? UITableViewHeaderFooterView headerView!.textLabel!.frame = CGRectMake(0, 10, tableView.frame.size.width, 25) headerView!.textLabel!.backgroundColor = UIColor(red: 230.0/255.0, green: 230.0/255.0, blue: 230.0/255.0, alpha: 1.0) headerView!.textLabel!.font = UIFont(name: "BrandonGrotesque-Medium", size: 10) headerView!.textLabel!.textColor = UIColor.blackColor() headerView!.textLabel!.textAlignment = .Center } func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { if Array(sectionToAddOnsDictionary.keys).count < 2 { return nil } var headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("MYOSHeaderView") if headerView == nil { tableView.registerClass(UITableViewHeaderFooterView.classForCoder(), forHeaderFooterViewReuseIdentifier: "MYOSHeaderView") headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("MYOSHeaderView") } configureHeaderView(headerView!, forSection: section) return headerView } 

对于Swift 3 ,你可以有这样的事情, if let来避免可选项,就可以加上

  func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { if (view is UITableViewHeaderFooterView) { if let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView { tableViewHeaderFooterView?.textLabel?.textAlignment = .center } } }