繁体   English   中英

UICollectionViewCell不适合设备屏幕

[英]UICollectionViewCell does not fit device screen

我试图使UICollectionViewCell适合屏幕宽度。

在界面构建器中,它看起来像这样:

当我在iPhone 6上构建并运行时,它看起来像这样:

(如您所见,我想在左侧和右侧填充宽度。

我尝试将其添加到cellForItemAtIndexPath

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

但这使单元格移到右侧,因此该单元格的一部分不在屏幕视图范围内。

我的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
    }

编辑

我尝试添加以下代码:

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

这样就使宽度合适,但是图​​像和文本没有移动: 此处的图像

您似乎是UIEdgeInsets的受害者,在iOS 8+上默认不为零。 您可以在Interface Builder中进行设置:

或在代码中:

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:

将委托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