具有布局约束的UicollectionView

时间:2015-07-09 07:55:53

标签: ios objective-c nslayoutconstraint nsautolayout

我在视图控制器中使用集合视图。

我没有在IB中使用自动布局,但是调整了集合视图的大小。

我的问题是在iPhone 4s正常工作。当我在iphone 6上运行时不合适。

我可以使用nslayout约束吗?我的imageview单元格大小宽度80和高度90是固定的。

任何可以解决所有设备问题的解决方案吗?

2 个答案:

答案 0 :(得分:0)

您可以自动调整您的集合视图,该视图适用于除iphone 4以外的所有iPhone设备,因此对于iPhone 4S,您可以通过编程方式设置其对齐方式。如

-(void)ViewDidLoad{
     //get the screen size of device
     screenHeight = [UIScreen mainScreen].bounds.size.height;
        if (screenHeight == 480){
        //Your code e.g
        <collectionView>.frame = CGRectMake((x-axis),(y-axis),width,height);
    }
}

答案 1 :(得分:0)

如果您没有使用autolayout,请通过属性设置自动调整遮罩

enter image description here

enter image description here

你也可以通过代码来完成:

view.autoresizingMask = (UIViewAutoresizingFlexibleWidth |    
                              UIViewAutoresizingFlexibleLeftMargin);

CELL RESIZE

如果要根据设备更改集合视图的单元格大小,请使用以下方法:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((collectionView.frame.size.width/4)-5, (collectionView.frame.size.width/4)-5);
}

这里5是你在单元格之间的填充。