【问题标题】:Scale table view cell height proportionally for all screens using autolayout使用自动布局为所有屏幕按比例缩放表格视图单元格高度
【发布时间】:2015-12-19 01:31:22
【问题描述】:

我在 .xib 中制作了一个简单的表格视图。我想为所有屏幕尺寸(即 iphone 6、6 plus 和 ipad)扩展表格视图单元格。目前我已经硬编码了不同屏幕的不同高度,如下所示:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:       (NSIndexPath *)indexPath{

   CGFloat cellHeight = 0;

    if (IS_IPHONE_6P) {

    // iphone 6P
   // cellHeight = 133;
    cellHeight = 175;


    }
    else if (IS_IPHONE_6){
    // on iphone 6
   // cellHeight =  120;
    cellHeight =  160;

    }

    else if (IS_IPHONE_5){
    //On iphone 5
   // cellHeight =  105;
    cellHeight =  145;

    }

    else if (IS_IPHONE_4_OR_LESS){
    //  on iphone 4
   // cellHeight =  50;
    cellHeight =  130;

    }
   else {
    //on ipad
   // cellHeight =  200;
    cellHeight =  280;

    }


   //  cellHeight = cellHeight *2;

     return  cellHeight;

     }

我有更好的方法吗?

【问题讨论】:

  • CGRectGetHeight([UIScreen mainScreen].bounds) * 比例

标签: ios objective-c swift uitableview ios8


【解决方案1】:

一种可能的解决方案是获取屏幕高度并乘以您的比率系数。

return CGRectGetHeight([UIScreen mainScreen].bounds) * ratio;

【讨论】:

  • 所以我需要在 heightForRowAtIndexPath 方法中这样做
  • 在这种情况下,ratio 是什么?
  • 塔什,屏幕百分比。如果您的 ratio = 1,您的单元格将与屏幕高度相同,ratio = 0.5 - 屏幕的一半单元格高度
【解决方案2】:

一种方法是获取您的UITableView 的高度并将其除以您正在寻找的比率。

您可以在viewDidLoadviewDidAppear 中获取UITableView 的高度

【讨论】:

  • 这两种方法太早了,你得到的高度是故事板高度,而不是设备上的最终高度。尝试使用 viewDidLayoutSubviews 来获取表格视图的实际高度。
猜你喜欢
  • 2015-06-16
  • 2014-10-01
  • 2013-05-16
  • 2015-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多