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

12

我觉得这将是一个简单的答案,涉及到AutoResizingMasks,但我似乎无法理解这个主题。

我有一个iPad应用程序,显示两个并排的UITableView。当我从纵向旋转到横向,然后再旋转回来时,UITableView中的单元格会在旋转发生时实时地完美调整大小。我正在使用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将重新调整大小并显示正确。我无法弄清楚的是如何正确设置AutoResizeMask属性,以便头部可以像单元格一样调整大小。


从你的问题中我并不清楚:问题是表头的宽度没有随着表格一起改变,还是你期望他们的高度改变,或者是发生的更改没有随着表格的旋转进行动画处理? - Sixten Otto
我很久没有看过这个了,但是在回顾时立刻发现了一个问题。我猜测我的问题与宽度灵活和左右边距灵活有关。谁知道如何在这种情况下预测水平调整大小的行为呢?我敢打赌,将AutoResizingMask设置为仅灵活宽度可能就是答案。不过,正如我下面所说的,我已经转向完全不同的解决方案。 - mbm29414
4个回答

15

这不是一个很好的解决方案。但能用:

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

这里使用的所有方法都是公共方法。 - ıɾuǝʞ
我正在显示一个右对齐的图像,这是一个好的解决方案。以下是Swift中的解决方案: override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { super.willAnimateRotationToInterfaceOrientation(toInterfaceOrientation, duration: duration) self.tableView.reloadData() } - philippinedev

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

0
我创建了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
}()

}

0
我很想得到一个真正的答案,但现在我只是重新设计了我的UITableView,使得我的“headers”只是表格内的单元格。这种方式调整大小没有问题。

我尝试过这种方式。但是如果您有多个单元格视图和部分,很难控制单元格。 - Kimi Chiu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接