简体   繁体   English

具有布局约束的UicollectionView

[英]UicollectionView with layout constraints

I am using collection view inside view controller. 我在视图控制器中使用集合视图。

I am not using auto layout in IB but resizing collection view. 我没有在IB中使用自动布局,而是在调整集合视图的大小。

My problem is it proper working in iphone 4s. 我的问题是在iphone 4s中正常工作。 when i run on iphone 6 is not proper . 当我在iPhone 6上运行时不正确。

Can i use nslayout constraint ? 我可以使用nslayout约束吗? My imageview cell size width 80 and height 90 is fix . 我的imageview单元格大小宽度80和高度90是固定的。

Any solution to work all devices ? 有什么解决方案可以使所有设备工作?

You can autoresize your collection view which will work for all the iPhone devices except iphone 4s, so for iPhone 4S you can set its alignment programmatically. 您可以自动调整集合视图的大小,该视图将适用于除iPhone 4s以外的所有iPhone设备,因此对于iPhone 4S,您可以通过编程设置其对齐方式。 Such as

-(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);
    }
}

If you are not using autolayout then, Set the autoresizing mask via properties 如果您不使用自动布局,请通过属性设置自动调整大小的蒙版

在此处输入图片说明

在此处输入图片说明

You can also do it by code : 您也可以通过代码进行操作:

view.autoresizingMask = (UIViewAutoresizingFlexibleWidth |    
                              UIViewAutoresizingFlexibleLeftMargin);

CELL RESIZE 电池尺寸

If you want to change cell size of collection view as per device then use following method : 如果要按设备更改收集视图的单元格大小,请使用以下方法:

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

Here 5 is your padding between cell. 这里5是您在单元格之间的填充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM