【问题标题】:Use Adaptive Layout programmatically to resize static cells height for hCompact以编程方式使用自适应布局调整 hCompact 的静态单元格高度
【发布时间】:2015-04-19 10:48:55
【问题描述】:

在纯肖像文字游戏中,我使用静态单元格来显示 IAP 商店:

您可以在上面的 iPhone 4 屏幕截图中看到我的问题 - 底部的粉红色按钮(用于观看视频广告并获得 150 个硬币)不可见。

这是我的 Xcode 截图(请点击fullscreen):

我使用 7 个静态单元格:

  • 带有返回按钮、标题、钱袋图标的蓝色顶部单元格
  • 状态文本(在上面的屏幕截图中不可见)
  • 硬币包 1
  • 硬币包 2
  • 硬币包 3
  • 硬币包 4
  • 视频广告(底部的粉色单元格 - 存在在 iPhone 4 和其他紧凑型设备上不可见的问题)

并使用此方法调整单元格的大小:

- (CGFloat)tableView:(UITableView *)tableView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0)
        return 70;

    if ([indexPath row] == 1)
        return 35;

    return 90; // XXX how to change this to 70 for hCompact?
}

我的问题是:如何以编程方式为高度紧凑的设备调整单元格高度(Adaptive Layout 中的 hCompact 大小类)。

更新:

到目前为止,我自己的丑陋解决方案是:

@interface StoreCoinsViewController ()
{
    int _cellHeight;
}

- (int)setCellHeight  // called in viewDidLoad
{
    int screenHeight = UIScreen.mainScreen.bounds.size.height;
    NSLog(@"screenHeight=%d", screenHeight);

    if (screenHeight >= 1024)  // iPad
        return 160;

    if (screenHeight >= 736)   // iPhone 6 Plus
        return 110;

    if (screenHeight >= 667)   // iPhone 6
        return 100;

    if (screenHeight >= 568)   // iPhone 5
        return 90;

    return 72;  // iPhone 4s (height=480) and earlier
}
- (CGFloat)tableView:(UITableView *)tableView
      heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath row] == 0)
        return 70;

    if ([indexPath row] == 1)
        return 35;

    return _cellHeight;
}

【问题讨论】:

  • 如果单元格是已知的,你不能让它快速而肮脏,并获得表格视图高度减去先前单元格高度的总和并将其返回给最后一个单元格吗?
  • 那么(如您在 iPhone 4 屏幕截图中所见)我将只剩下几个像素留给最后一个单元格。我更愿意为 all 单元格使用较小的高度 - 在 hCompact 设备上。
  • 哦,好吧,我知道了,恐怕使用 ios8 和Auto sizing cells 你可能会找到一个(肮脏的)解决方案,但对于旧操作系统,你必须“手动”进行数学运算。我真的很感兴趣。

标签: ios iphone size-classes adaptive-layout


【解决方案1】:

我会写一个帮助器来查看当前特征集合的垂直大小类

- (CGFloat)verticalSizeForCurrentTraitCollection {
    switch (self.traitCollection.verticalSizeClass) {
        case UIUserInterfaceSizeClassCompact:
            return 70;
        case UIUserInterfaceSizeClassRegular:
            return 90;
        case UIUserInterfaceSizeClassUnspecified:
            return 80;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath row] == 0)
        return 70;

    if ([indexPath row] == 1)
        return 35;

    return [self verticalSizeForCurrentTraitCollection];
}

【讨论】:

  • 谢谢。出于某种原因,我为 iPhone 4s、iPhone 6、iPad 2、iPad Retina(所有 - 纵向模式的模拟器)获得了 UIUserInterfaceSizeClassRegular - 这使得这种方法无法用于我的应用程序......
【解决方案2】:

每个 UIViewController 都有一个可以在代码中使用的 traitCollection 属性。

在你的情况下,你可以这样检查:

if self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact {
    return 70;
}
else {
    return 90
}

【讨论】:

    【解决方案3】:

    无论如何,在这种情况下,作为一种解决方法,单元格数量如此之少,带有子视图容器的滚动视图将完全满足您的需求。我知道更多的代码,但这并不比手动计算每个单元格的高度更糟糕。等待更好的解决方案。

    【讨论】:

    • 不,我的意思是根本不使用表格视图。
    • 哦,好的,我明白了。修复底部最后一个“单元格”,中间有一个滚动视图
    • 完全正确,或者任何想要的组合。您甚至可能只想要一个带有 5 个子视图(旧单元格)的静态视图
    • 是的,或者你想要的任何组合,实际上你甚至可能只有一个包含 5 个子视图容器单元格的视图并轻松应用“病态”布局
    猜你喜欢
    • 1970-01-01
    • 2015-04-28
    • 2016-11-14
    • 2020-05-31
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2015-02-19
    • 2016-07-13
    相关资源
    最近更新 更多