Santanu Karar
Santanu Karar

Reputation: 1076

Using TVPosterView inside UICollectionViewCell creates odd sizing issue

I'm trying to create a tvOS project using UICollectionView displays camera streams.

In the UICollectionViewCell I'm using TVPosterView to display the stream/image and a title.

The problem that I'm facing currently that when the app starts all thumbnails/UICollectionViewCell seems to have proper size; First item focused with larger size than others. When I roll-out to other item/s, the earlier focused item became much smaller than the size it has when started.

Let me try to explain the situation if I'm not clear with my words.

When the app starts - Camera A focused and larger size than others:

enter image description here

When roll-out Camera A, it become much smaller than Camera D or Camera E; Also, the currently focused Camera B do not has a larger size anymore (if I compared with Camera A size from previous screen):

enter image description here

In the UICollectionView controller I have following delegate method:

extension ListingViewController: UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let thumbWidth = CGFloat(320)
        let thumbHeight = CGFloat(thumbWidth * 0.7)
        return CGSize(width: thumbWidth, height: thumbHeight)
    }
}

In the UICollectionViewCell I basically has following codes:

internal func setupUI()
{
        posterView = TVPosterView()
        posterView.tag = 1100
        posterView.clipsToBounds = false
        
        addSubview(posterView)
        posterView.alignToSuperView()
}

alignToSuperView() basically an extension to UIView with following codes:

public func alignToSuperView()
{
        self.translatesAutoresizingMaskIntoConstraints = false
        guard let margins = self.superview?.layoutMarginsGuide else {
            return
        }
        self.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        self.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        self.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        self.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
        self.superview?.layoutIfNeeded()
}

Upvotes: 1

Views: 82

Answers (0)

Related Questions