旋转时自动调整UITableView标题(主要在iPad上)

mbm*_*414 11 resize header mask uitableview ios

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

我有一个iPad应用程序并排显示2个UITableViews.当我从纵向旋转到横向并返回时,UITableView中的单元格在旋转发生时即时完美地调整大小.我正在使用UITableViewCellStyleSubtitle UITableViewCells(暂时不是子类),我在IB中设置UITableView以锚定到顶部,左侧和底部边缘(对于左UITableView)并具有灵活的宽度.

我正在提供我自己的UIView对象

- (UIView *)tableView:(UITableView *)tableView 
     viewForHeaderInSection:(NSInteger)section
Run Code Online (Sandbox Code Playgroud)

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

+ (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;
}
Run Code Online (Sandbox Code Playgroud)

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

ıɾu*_*uǝʞ 15

不是一个很好的解决方案.但是作品:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [mTableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)


was*_*faq 5

我最近遇到了同样的问题.诀窍是使用自定义视图作为表的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
Run Code Online (Sandbox Code Playgroud)