首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将集合显示为每个设备ios的屏幕大小的一半

无法将集合显示为每个设备ios的屏幕大小的一半
EN

Stack Overflow用户
提问于 2022-09-15 06:54:25
回答 1查看 36关注 0票数 0

在图像中,它们是uiCollection view.And的两项之间的很多s空间,它们并不适合每个设备大小的适当显示。我试图在一行中显示两项集合,但是它不能正确地显示每个设备,如何快速地将屏幕分割成两个部分,我使用了几个约束,但它不起作用,我对UITableview也有类似的问题。

代码语言:javascript
复制
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)

        

    }
    
    
    
    
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-15 07:25:13

考虑到集合视图的前导和尾随为0,在视图中需要实现以下UICollectionViewDelegateFlowLayout方法:

代码语言:javascript
复制
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)
    
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73726743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档