在图像中,它们是uiCollection view.And的两项之间的很多s空间,它们并不适合每个设备大小的适当显示。我试图在一行中显示两项集合,但是它不能正确地显示每个设备,如何快速地将屏幕分割成两个部分,我使用了几个约束,但它不起作用,我对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)
}
}发布于 2022-09-15 07:25:13
考虑到集合视图的前导和尾随为0,在视图中需要实现以下UICollectionViewDelegateFlowLayout方法:
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)
}
}https://stackoverflow.com/questions/73726743
复制相似问题