简体   繁体   English

UICollectionViewCell不适合设备屏幕

[英]UICollectionViewCell does not fit device screen

I am trying to make the UICollectionViewCell fit the screen width. 我试图使UICollectionViewCell适合屏幕宽度。

In the interface builder, it looks like this: 在界面构建器中,它看起来像这样:

When I build and run on iPhone 6, it looks like this: 当我在iPhone 6上构建并运行时,它看起来像这样:

(As you can see, I want to fill the width on left and right side. (如您所见,我想在左侧和右侧填充宽度。

I tried adding this in the cellForItemAtIndexPath : 我尝试将其添加到cellForItemAtIndexPath

cell.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.size.width, cell.bounds.size.height)

But this made the cell move to the right side, so a part of the cell it out of the screen view.. 但这使单元格移到右侧,因此该单元格的一部分不在屏幕视图范围内。

My cellForItemAtIndexPath looks like this: 我的cellForItemAtIndexPath看起来像这样:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionViewCell  

        cell.frame.size = CGSizeMake(UIScreen.mainScreen().bounds.size.width, cell.bounds.size.height)

        cell.flagContentButton.titleLabel?.adjustsFontSizeToFitWidth = true
        cell.uploadedTimeLabel.adjustsFontSizeToFitWidth = true
        cell.imageText.adjustsFontSizeToFitWidth = true

        cell.layer.borderWidth = 0.5
        cell.layer.cornerRadius = 3
        let myColor : UIColor = UIColor(red: 0.0, green: 0.0, blue:0.0, alpha: 0.15)
        cell.backgroundColor = UIColor.whiteColor()
        cell.layer.borderColor = myColor.CGColor

        if (self.arrayOfDetails.count > indexPath.row){
            let post = self.arrayOfDetails[indexPath.row]
            cell.imageText.text = post.text
            cell.objectID.append(post.objID)
            cell.uploadedTimeLabel.text = post.CreatedAt.timeAgo

            cell.imageView.setImageWithUrl(NSURL(string: post.image)!, placeHolderImage: UIImage(named: "Placeholder"))
        }

        return cell
    }

EDIT : 编辑

I tried adding this code: 我尝试添加以下代码:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        // Code below
        return CGSizeMake(UIScreen.mainScreen().bounds.size.width, 126)
    }

That made the width fit good, but the image and text did not move: Image here 这样就使宽度合适,但是图​​像和文本没有移动: 此处的图像

Looks like you're a victim of UIEdgeInsets defaulting to not zero on iOS 8+. 您似乎是UIEdgeInsets的受害者,在iOS 8+上默认不为零。 You can set these in Interface Builder: 您可以在Interface Builder中进行设置:

Or in code: 或在代码中:

UITableView: UITableView:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

UICollectionView: UICollectionView:

Add the delegate UICollectionViewDelegateFlowLayout to your view controller interface, then: 将委托UICollectionViewDelegateFlowLayout添加到视图控制器界面,然后:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

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

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