Ninder Chand
Ninder Chand

Reputation: 409

Auto height For ipad And iphone

I am making app with Xcode but my problem is that when i open app on iPad it shrinks the height and i want to add auto height for iPad and iPhone. This is my code

 func collectionView(_ collectionView:UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        return CGSize(width: view.frame.width-20 , height: 200)
    }

and this is cellview

 override func awakeFromNib() {
        super.awakeFromNib()

        title.numberOfLines = 2
        let layer = CAGradientLayer()
        layer.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 400)
        let top = UIColor.clear.cgColor
        let bottom = UIColor(red: 0 / 255, green: 0 / 255, blue: 0 / 255, alpha: 1.0).cgColor
        layer.colors = [top, bottom]
        layer.opacity = 1.7
        imageArticle.layer.insertSublayer(layer, at: 0)
        self.layer.cornerRadius = 4.0

thankyou

For iPhone it looks like this enter image description here

For iPad height looks like this enter image description here

Upvotes: 0

Views: 109

Answers (2)

Dilan
Dilan

Reputation: 2688

Use device height as collectionview cell

func collectionView(_ collectionView:UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        return CGSize(width: view.frame.width-20 , height: view.frame.height)
}

Upvotes: 0

Picode
Picode

Reputation: 1158

You can use this :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if UIScreen.main.bounds.size.height < 570{ //Write iPhone or iPad size. If iPad :
        return CGSize(width: (((collectionView.frame.width) - 20) / 2), height: (collectionView.frame.height) - 70)
    } else { //if iPhone
        return CGSize(width: (((collectionView.frame.width) - 20) / 2), height: (collectionView.frame.height) / 2)
    }
}

Set width and height whatever you need.

Hope it helps...

Upvotes: 1

Related Questions