I am currently using UICollectionView for the user interface grid, and it works fine. However, I'd like to be enable horizontal scrolling. The grid supports 8 items per page and when the total number of items are, say 4, this is how the items should be arranged with horizontal scroll direction enabled:
0 0 x x
0 0 x x
Run Code Online (Sandbox Code Playgroud)
Here 0 -> Collection Item and x -> Empty Cells
有没有办法使它们居中对齐,如:
x 0 0 x
x 0 0 x
Run Code Online (Sandbox Code Playgroud)
这样内容看起来更干净? …
我有一个流式布局的UICollectionView,每个单元格都是一个正方形.如何确定每行上每个单元格之间的间距?我似乎找不到适合的设置.我看到nib文件中有一个min间距属性用于集合视图,但我将其设置为0并且单元格甚至没有粘贴.

还有其他想法吗?
我有UICollectionView随机细胞.有没有什么方法可以让我居中行?
这是默认情况下的外观:
[ x x x x x x ]
[ x x x x x x ]
[ x x ]
Run Code Online (Sandbox Code Playgroud)
这是所需的布局:
[ x x x x x ]
[ x x x x x ]
[ x x ]
Run Code Online (Sandbox Code Playgroud) 这是导致警告的代码:
private override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItemAtIndexPath(indexPath)
let distance = CGRectGetMidX(attributes!.frame) - self.midX;
var transform = CATransform3DIdentity;
transform = CATransform3DTranslate(transform, -distance, 0, -self.width);
attributes!.transform3D = CATransform3DIdentity;
return attributes
}
Run Code Online (Sandbox Code Playgroud)
控制台还打印:
这可能是因为流布局"xyz"正在修改UICollectionViewFlowLayout返回的属性而不复制它们.
我该如何修复此警告?
我有一个位于 tableView 单元格内的 collectionView。collectionView 有多个部分。如果一节只有一个单元格,则该单元格会居中显示。如何将单个单元格与左侧对齐?
collectionView 具有以下内容flowLayout:
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
collectionView.setCollectionViewLayout(flowLayout, animated: true)
Run Code Online (Sandbox Code Playgroud) uitableview uicollectionview uicollectionviewcell uicollectionviewlayout swift
我想实现一个TokenView(芯片视图)样式视图.为此我在UITableViewCell中使用了一个集合视图,CollectionView有自定义cell.Inside自定义单元格UILabel.现在,单元格的宽度取决于内容UILabel.
下面是自定义UITableViewCell Cell的代码
import UIKit
class LanguageTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
var arr:[String] = ["English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate","English","Intermediate"]
@IBOutlet weak var collectionView: UICollectionView!
override func awakeFromNib() {
super.awakeFromNib()
collectionView.dataSource = self
collectionView.delegate = self
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.scrollDirection = .vertical
self.collectionView.collectionViewLayout = layout
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> …Run Code Online (Sandbox Code Playgroud) 如何使UlCollectionView的项目位于行内左侧
这种位置(上方屏幕截图)只能在线路上有唯一项目的情况下进行; 如果线上有多个元素(项目较小或屏幕较宽),则元素在行内左对齐并且位置看起来像所需:
UICollectionViewFlowLayout的设置如下:
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = 20.;
flowLayout.minimumInteritemSpacing = 5.;
flowLayout.sectionInset = UIEdgeInsetsMake(23., 15., 23., 15.);
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView.collectionViewLayout = flowLayout;
Run Code Online (Sandbox Code Playgroud) 如何删除两个单元格之间的空间UiCollectionView?最小间距不起作用.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (collectionView.tag==101)
{
return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));
}
else
{
return CGSizeMake((_collView2.frame.size.width/3)-20,300);
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
我有一个像这样的UICollectionView:
这是三种不同的情况.浅灰色显示集合视图的边框,深灰色显示单元格.我已经将min spacing和section insets设置为0.但是我仍然得到这些不需要的插入内容,并且它似乎只有在有超过1个单元格时才会发生.
我像这样计算项目大小:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
guard let item = place?.publicTransports?[indexPath.row] else {
return CGSize.zero
}
var lines = ""
for (i, line) in item.lines.enumerate() {
lines.appendContentsOf(line)
if i != item.lines.count - 1 {
lines.appendContentsOf(", ")
}
}
let linesString = lines as NSString
return CGSize(width: linesString.sizeWithAttributes(nil).width + 35 + 20, height: collectionView.bounds.height/2)
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?