Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self sizing UIImageView #1195

Closed
2 tasks done
onurgenes opened this issue Jun 20, 2018 · 1 comment
Closed
2 tasks done

Self sizing UIImageView #1195

onurgenes opened this issue Jun 20, 2018 · 1 comment
Labels

Comments

@onurgenes
Copy link

onurgenes commented Jun 20, 2018

New issue checklist

General information

  • IGListKit version: 3.4.0
  • iOS version(s): 11.4
  • CocoaPods/Carthage version: 1.5.3
  • Xcode version: 9.4.1
  • Devices/Simulators affected: iPhone X, Simulator
  • Reproducible in the demo project? (Yes/No): Yes
  • Related issues: Can't dynamic sizing UIImageView

Debug information

I am trying to implement IGListKit based CollectionView. It has 1 UILabel and 1 UIImageView.

Checked official examples too but couldn't manage to make it self sizing.

This is Cell class. In this class tried to add the code which is given in the official repository. The function I've used is 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
    }
    }

This is 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
    }
    }

And lastly Controller:

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

What I am trying to implemet is making a Instagram like layout but less complicated. I won't add new views or anything.

In the preferredLayoutAttributesFitting method, I am getting the original image size. Not the scaled image's size.

The official examples are made with just labels. Before trying to implement this project I tried with just labels and constant sized UIImageView for profile photo. It is working good with it.

Not sure if the problem is with SteviaLayout?

Thanks in advance.

@rnystrom
Copy link
Contributor

Unfortunately going to close since this is a layout issue and doesn’t seem to be an issue/question particularly about IGListKit. I’d check out #516 for known issues with self sizing cells.

Sent with GitHawk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants