自动resize的UITableView头旋转(大部分在iPad上)

我觉得这将是围绕着AutoResizingMasks的一个简单的答案,但我似乎无法围绕这个话题。

我有一个显示2个UITableView的iPad应用程序。 当我从纵向旋转到横向时,UITableView中的单元格会在旋转发生的同时完美地resize。 我正在使用UITableViewCellStyleSubtitle UITableViewCells(不是现在的子类),而且我已经在IB中设置了UITableView以锚定到顶部,左侧和底部边缘(对于左边的UITableView)并且具有灵活的宽度。

我提供我自己的UIView对象

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

这是我到目前为止(称为另一个类的类方法):

 + (UIView *)headerForTableView:(UITableView *)tv { // The view to return UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [tv frame].size.width, someHeight)]; [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin]; // Other layout logic... doesn't seem to be the culprit // Return the HeaderView return headerView; } 

所以,无论在哪个方向,一切都会像我想要的那样加载。 旋转后,如果我手动调用reloadData或等到我的应用程序触发它,或滚动UITableView,headerViews将resize,并正确显示自己。 我无法弄清楚的是如何正确获取AutoResizeMask属性设置,以便标题将像单元格一样resize。

不是一个很好的修复。 但作品:

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; [mTableView reloadData]; } 

我最近面临同样的问题。 诀窍是使用自定义视图作为表的headerView。 覆盖layoutSubviews允许我随意控制布局。 下面是一个例子。

 #import "TableSectionHeader.h" @implementation TableSectionHeader - (id)initWithFrame:(CGRect)frame title:(NSString *)title { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; // Initialization code headerLabel = [[UILabel alloc] initWithFrame:frame]; headerLabel.text = title; headerLabel.textColor = [UIColor blackColor]; headerLabel.font = [UIFont boldSystemFontOfSize:17]; headerLabel.backgroundColor = [UIColor clearColor]; [self addSubview:headerLabel]; } return self; } -(void)dealloc { [headerLabel release]; [super dealloc]; } -(void)layoutSubviews { [super layoutSubviews]; NSInteger xOffset = ((55.0f / 768.0f) * self.bounds.size.width); if (xOffset > 55.0f) { xOffset = 55.0f; } headerLabel.frame = CGRectMake(xOffset, 15, self.bounds.size.width - xOffset * 2, 20); } +(UIView *) tableSectionHeaderWithText:(NSString *) text bounds:(CGRect)bounds { TableSectionHeader *header = [[[TableSectionHeader alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, 40) title:text] autorelease]; return header; } +(CGFloat) tableSectionHeaderHeight { return 40.0; } @end 

我很想得到一个真正的答案,但现在,我刚刚重新工作我的UITableView,使我的“标题”只是在表内的单元格。 resize没有这样的问题。

我已经创build了UIView子类,我已经使用视觉约束将子视图粘贴到屏幕的两侧。 和旋转都很好。

 class MLFlexibleView: UIView { override init(frame: CGRect) { super.init(frame: frame) self.addSubview(self.segmentedControl) self.setUpConstraints() } func setUpConstraints() { let views = ["view" : self.segmentedControl] NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-7-[view]-7-|", options: [], metrics: nil, views: views)) NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[view]-15-|", options: [], metrics: nil, views: views)) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } let segmentedControl:UISegmentedControl = { let segmentedControl = UISegmentedControl(items: ["Searches".localized, "Adverts".localized]) segmentedControl.selectedSegmentIndex = 0 segmentedControl.tintColor = UIColor.white segmentedControl.translatesAutoresizingMaskIntoConstraints = false return segmentedControl }() }