Not able to display collection to half of size of the screen for every device ios

Viewed 31

In Image their is lot os space between the two items of uiCollection view.And they are not display properly for every device sizeI am trying to Display collection two items at one row but it doesn't display properly for every devices how can I split screen into two part in swift.I have use several Constraint but it doesn't work I also have similar problem for UITableview.

import UIKit

class MarketProduct: UIViewController {

    @IBOutlet weak var searchbar: UISearchBar!
    @IBOutlet weak var ItemCollectionView: UICollectionView!
    let productImages:[String]=["pulse","pulse","pulse","pulse","pulse","pulse","pulse","pulse","pulse","pulse","pulse"];
    let productPrice:[String]=["$20000","$20000","$20000","$20000","$20000","$20000","$20000","$20000","$20000","$20000","$20000"]
    let productDescription:[String]=["Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements","Server-side rendering with JavaScript libraries like React is where the server returns a ready to render HTML page and the JS scripts required to make the page interactive. The HTML is rendered immediately with all the static elements"]
    let productAge:[String]=["2 years old","2 years old","2 years old","2 years old","2 years old","2 years old","2 years old","2 years old","2 years old","2 years old","2 years old"]
    let locations:[String]=["pune","pune","pune","pune","pune","pune","pune","pune","pune","pune","pune"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        searchbar.searchTextField.backgroundColor = .white
        searchbar.layer.borderWidth = 1
        searchbar.layer.borderColor = UIColor.black.cgColor
        searchbar.layer.cornerRadius = 15
        searchbar.layer.masksToBounds = true
 
    }
    
}
extension MarketProduct:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        productImages.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = ItemCollectionView.dequeueReusableCell(withReuseIdentifier:"ItemCell", for: indexPath) as!  Items
        cell.itemImage.image = UIImage(named: productImages[indexPath.row])
        cell.PriceLabel.text = productPrice[indexPath.row]
        cell.descriptionLabel.text = productDescription[indexPath.row]
        cell.itemLocation.text = locations[indexPath.row]
        cell.layer.cornerRadius = 20
        cell.layer.borderWidth = 1
        cell.layer.borderColor = UIColor.blue.cgColor
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
//          let height = collectionView.frame.height
//            let width  = collectionView.frame.width
//            return CGSize(width: width, height: height)
//          let height = view.frame.size.height
          let width = view.frame.size.width
      
          // in case you you want the cell to be 40% of your controllers view
        return CGSize(width: width * 0.4, height: 360)

        

    }
    
    
    
    
}
1 Answers

Considering collection view leading and trailing is 0 with the view you need to implement the following UICollectionViewDelegateFlowLayout methods:

extension  MarketProduct: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width/2, height: 360)
    
    }
}
Related