为了在我的 View 和设备之间获得一致的大小比例,我在 View 和父 View 之间设置了相等的宽度/高度约束。但是,在设计 UITableViewCell 或 UICollectionViewCell 时,我无法在界面生成器中找到此选项。我需要设置单元格内的 View 与 super View 之间的比率。完成此行为的最干净的方法是什么?
最佳答案
您不能在 Storyboard 中执行此操作,而必须在代码中执行:
对于UITableView:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return tableView.frame.size.width;
}
对于UICollectionView:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
int columns = 2; // Change this to whatever you want
float width = collectionView.frame.size.width / columns;
return CGSizeMake(width, width);
}
关于ios uitableviewcell 项目与 superview Storyboard界面生成器的高度相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211291/