iOS-IGListKit-使用UIImageView

时间:2018-06-20 06:17:37

标签: ios swift iglistkit

我正在尝试实现基于IGListKit的CollectionView。它有1个UILabel和1个UIImageView。

也检查了官方示例,但无法使其自动调整大小。

这是Cell类。在此类中,尝试添加官方存储库中提供的代码。我使用的函数是preferredLayoutAttributesFitting:

import UIKit
import Stevia

class MainControllerCell: UICollectionViewCell {

lazy var userNameLabel: UILabel = {
    let lbl = UILabel()
    lbl.numberOfLines = 1
    lbl.translatesAutoresizingMaskIntoConstraints = false
    return lbl
}()

lazy var photoImageView: UIImageView = {
    let iv = UIImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    return iv
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    contentView.backgroundColor = .white

    contentView.sv(
        userNameLabel,
        photoImageView
    )   
}

override func layoutSubviews() {
    contentView.layout(
        8,
        |-8-userNameLabel.height(20)-8-|,
        8,
        |-0-photoImageView-0-|,
        8
    )
}

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {

    setNeedsLayout()
    layoutIfNeeded()
    let size = contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
    var newFrame = layoutAttributes.frame
    // note: don't change the width
    newFrame.size.height = ceil(size.height)
    layoutAttributes.frame = newFrame
    return layoutAttributes
}
}

这是SectionController:

import IGListKit

class MainSectionController: ListSectionController {

private var post: Post!

override init() {
    super.init()
    inset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)
    minimumLineSpacing = 10
    minimumInteritemSpacing = 10
}

override func numberOfItems() -> Int {
    return 1
}

override func sizeForItem(at index: Int) -> CGSize {
    let width = collectionContext!.containerSize.width
    return CGSize(width: width, height: 75) // width / post.photo.getImageRatio() + 44
}

override func cellForItem(at index: Int) -> UICollectionViewCell {
    let userName = post.userName
    let photo = post.photo
    let cell: UICollectionViewCell

    guard let mainCollectionViewCell = collectionContext?.dequeueReusableCell(of: MainControllerCell.self,
                                                                              for: self,
                                                                              at: index) as? MainControllerCell else { fatalError() }

    mainCollectionViewCell.userName = userName
    mainCollectionViewCell.photoImageView.image = photo
    cell = mainCollectionViewCell

    return cell
}

override func didUpdate(to object: Any) {
    self.post = object as? Post
}
}

最后是控制器:

import UIKit
import IGListKit

class MainController: UIViewController, ListAdapterDataSource {

lazy var adapter: ListAdapter = {
    let updater = ListAdapterUpdater()
    return ListAdapter(updater: updater, viewController: self)
}()

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.backgroundColor = .white
    return collectionView
}()

var posts: [Post] = []

override func viewDidLoad() {
    super.viewDidLoad()

    posts = [Post(timeStamp: 0, userName: "onur", photoUrl: "xxx", photo: UIImage(named: "cat")!),
             Post(timeStamp: 1, userName: "onur", photoUrl: "xxx", photo: UIImage(named: "iPhoneMP")!),
             Post(timeStamp: 2, userName: "onur", photoUrl: "xxx", photo: UIImage(named: "placeholder")!),
             Post(timeStamp: 3, userName: "onur", photoUrl: "xxx", photo: UIImage(named: "random")!)]

    adapter.collectionView = collectionView
    adapter.dataSource = self

    view.addSubview(collectionView)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    collectionView.frame = view.bounds

    adapter.performUpdates(animated: false, completion: nil)
}

// MARK: ListAdapterDataSource

func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
    return posts
}

func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
    return MainSectionController()
}

func emptyView(for listAdapter: ListAdapter) -> UIView? {
    return nil
}

}

我要实现的是使Instagram布局更简单。我不会添加新视图或其他任何内容。

在preferredLayoutAttributesFitting方法中,我正在获取原始图像大小。不是缩放图像的大小。

官方示例仅带有标签。在尝试实施此项目之前,我尝试仅使用标签和恒定大小的UIImageView作为个人资料照片。很好用。

不确定SteviaLayout是否存在问题?

谢谢。

0 个答案:

没有答案