collectionview 边距 collectionViewCell圆角

本文介绍了如何在iOS开发中使用UICollectionView,详细讲解了设置cell间间距的方法及collectionView视图边缘间距调整技巧,并演示了如何为UICollectionViewCell添加圆角。

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{}

这个方法只提供设置cell之间的距离,最左边的cell和最右边的cell到superview的间距却不能设置。

这样有两种方法来设置

1.改变collectionview的大小

2.设置flowLayout

flowLayout.sectionInset =UIEdgeInsetsMake(0,10,0,10);


collectionVIewCell圆角的设置

cell.contentView.layer.cornerRadius =10;

cell.layer.cornerRadius =10;

cell.contentView.layer.masksToBounds =YES;



// // OnlineDeviceListViewController2.swift // SurveillanceHome // // Created by MaCong on 2025/10/10. // Copyright © 2025 tplink. All rights reserved. // import UIKit class OnlineDeviceListViewController2: OnlineDeviceListViewController { private var _collectionView: UICollectionView? // 替换为图片数据(假设是 UIImage 数组) private let images = [ UIImage(named: "photo_icon")!, UIImage(named: "camera_icon")!, UIImage(named: "video_icon")!, UIImage(named: "mic_icon")! ] override var collectionView: UICollectionView { get { if let collectionView = _collectionView { return collectionView } let layout = UICollectionViewFlowLayout() // 设置两列布局 let itemSpacing: CGFloat = 10 let columnCount: CGFloat = 2 let totalSpacing = (columnCount - 1) * itemSpacing let itemWidth = (UIScreen.main.bounds.width - 20 - totalSpacing) / columnCount // 减去左右 layout.itemSize = CGSize(width: itemWidth, height: itemWidth) // 正方形单元格 layout.minimumInteritemSpacing = itemSpacing // 列间 layout.minimumLineSpacing = itemSpacing // 行间 let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) cv.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell") _collectionView = cv return cv } set { _collectionView = newValue } } override func viewDidLoad() { super.viewDidLoad() setupCollectionView() } private func setupCollectionView() { collectionView.backgroundColor = .clear collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.dataSource = self collectionView.delegate = self view.addSubview(collectionView) NSLayoutConstraint.activate([ collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20), collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10), collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) } } // MARK: - UICollectionViewDataSource @available(iOS 13.0, *) extension OnlineDeviceListViewController2: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return images.count // 4张图片 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell cell.configure(with: images[indexPath.item]) return cell } } // MARK: - 自定义图片 Cell class ImageCell: UICollectionViewCell { private let imageView = UIImageView() override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupUI() { backgroundColor = .lightGray layer.cornerRadius = 8 clipsToBounds = true // 确保圆角生效 imageView.contentMode = .scaleAspectFit imageView.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(imageView) NSLayoutConstraint.activate([ imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8), imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8), imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8) ]) } func configure(with image: UIImage) { imageView.image = image } } 修改为兼容所有版本的
最新发布
10-12
// // OnlineDeviceListViewController2.swift // SurveillanceHome // // Created by MaCong on 2025/10/10. // Copyright © 2025 tplink. All rights reserved. // import UIKit class OnlineDeviceListViewController2: DeviceListViewController { private var _collectionView: UICollectionView? private let numbers = ["1", "2", "3", "4"] // 添加数据源 override var collectionView: UICollectionView { get { if let collectionView = _collectionView { return collectionView } let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width: 100, height: 100) let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) cv.register(NumberCell.self, forCellWithReuseIdentifier: "NumberCell") _collectionView = cv return cv } set { _collectionView = newValue } } override func viewDidLoad() { super.viewDidLoad() setupCollectionView() } private func setupCollectionView() { collectionView.backgroundColor = .clear collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.dataSource = self collectionView.delegate = self view.addSubview(collectionView) NSLayoutConstraint.activate([ collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20), collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10), collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10), collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) } } // MARK: - UICollectionViewDataSource extension OnlineDeviceListViewController2: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return numbers.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NumberCell", for: indexPath) as! NumberCell cell.configure(with: numbers[indexPath.item]) return cell } } // MARK: - 自定义 Cell class NumberCell: UICollectionViewCell { private let label = UILabel() override init(frame: CGRect) { super.init(frame: frame) setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupUI() { backgroundColor = .lightGray layer.cornerRadius = 8 label.textAlignment = .center label.font = UIFont.boldSystemFont(ofSize: 24) label.translatesAutoresizingMaskIntoConstraints = false contentView.addSubview(label) NSLayoutConstraint.activate([ label.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor) ]) } func configure(with text: String) { label.text = text } } 将这换成两列图片 一共四张
10-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值