ios - UICollectionViewCell 不适合设备屏幕

标签 ios xcode swift uicollectionview

我正在尝试使 UICollectionViewCell 适合屏幕宽度。

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

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

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

我尝试将其添加到 cellForItemAtIndexPath 中:

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

但这会使单元格移到右侧,因此单元格的一部分超出了屏幕 View 。

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

这使得宽度适合,但图像和文本没有移动: Image here

最佳答案

看起来您是 iOS 8+ 上 UIEdgeInsets 默认不为零的受害者。您可以在 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:

将委托(delegate)UICollectionViewDelegateFlowLayout添加到您的 View Controller 界面,然后:

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

关于ios - UICollectionViewCell 不适合设备屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33769222/

相关文章:

android - 无效的资源目录名称 obj\Debug\res menu.png

objective-c - 开发一个在用户使用 xcode 登录之前运行的 osx 守护进程

ios - 应用程序在负载终止时崩溃,并出现 NSException 类型的未捕获异常

ios - UITableViews 之间的间距,如设置应用程序

ios - UIPickerView:NSAttributedString 在 iOS 7 中不可用?

iOS:检测用户向左/向右移动手机(不是旋转,而是移动)

ios - SwiftUI - Firebase 身份验证 - 环境对象不会在登录时更新

通过 JNLP 使用 Jenkins 时,iOS 测试不会在模拟器上运行

ios - 在单独类的对象上调用 Swift 选择器

swift - 我们可以在 SWIFT 中使用关键字作为参数名称吗?