简体   繁体   中英

UicollectionView with layout constraints

I am using collection view inside view controller.

I am not using auto layout in IB but resizing collection view.

My problem is it proper working in iphone 4s. when i run on iphone 6 is not proper .

Can i use nslayout constraint ? My imageview cell size width 80 and height 90 is fix .

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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