Reputation: 409
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 iPad height looks like this
Upvotes: 0
Views: 109
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
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