【问题标题】:Auto-Resize UITableView Headers on Rotate (Mostly on iPad)旋转时自动调整 UITableView 标题的大小(主要在 iPad 上)
【发布时间】:2011-12-12 03:23:19
【问题描述】:

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

我有一个 iPad 应用程序,它并排显示 2 个 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 属性,以便标题将像单元格一样调整大小。

【问题讨论】:

  • 从您的问题中我不清楚:是标题的宽度没有随表格变化的问题,您希望它们的高度发生变化,或者发生的变化没有随着桌子的旋转被动画化?
  • 我已经很久没看过这个了,但是一看到我就立刻跳出来了。我猜我的问题与灵活的宽度和灵活的左右边距有关。谁知道在这种情况下如何预测水平调整大小的行为?我敢打赌,将 AutoResizingMask 设置为灵活的宽度就是答案。不过,正如我在下面所说的,我再次转向了一个完全不同的解决方案。

标签: ios uitableview header resize mask


【解决方案1】:

不是一个很好的解决方法。但有效:

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

【讨论】:

  • 这里使用的方法都是公开的
  • 我正在显示一个右对齐的图像,这是一个很好的解决方案。这是 Swift 中的解决方案: override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { super.willAnimateRotationToInterfaceOrientation(toInterfaceOrientation, duration: duration) self.tableView.reloadData() }
【解决方案2】:

我最近遇到了同样的问题。诀窍是使用自定义视图作为表的 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

【讨论】:

    【解决方案3】:

    我很想得到一个真正的答案,但现在,我刚刚重新设计了我的 UITableView,以便我的“标题”只是表格中的单元格。这样调整大小没有问题。

    【讨论】:

    • 我试过这种方式。但是,如果您有多个单元格视图和部分,则很难控制单元格。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多