我正在尝试在UICollectionViewController中处理接口方向更改.我想要实现的是,我想在接口旋转后拥有相同的 contentOffset.意思是,它应该根据边界变化的比例进行更改.
从肖像开始,内容偏移量为{ bounds.size.width*2,0} ...

...应该导致景观中的内容偏移{ bounds.size.width*2,0}(反之亦然).

计算新的偏移量不是问题,但不知道,在何处(或何时)设置它以获得平滑的动画.我正在做的事情是使布局无效willRotateToInterfaceOrientation:duration:并重置内容偏移didRotateFromInterfaceOrientation::
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration;
{
self.scrollPositionBeforeRotation = CGPointMake(self.collectionView.contentOffset.x / self.collectionView.contentSize.width,
self.collectionView.contentOffset.y / self.collectionView.contentSize.height);
[self.collectionView.collectionViewLayout invalidateLayout];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
{
CGPoint newContentOffset = CGPointMake(self.scrollPositionBeforeRotation.x * self.collectionView.contentSize.width,
self.scrollPositionBeforeRotation.y * self.collectionView.contentSize.height);
[self.collectionView newContentOffset animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
这会在旋转后更改内容偏移.
如何在旋转期间设置它?我试图设置新的内容偏移量,willAnimateRotationToInterfaceOrientation:duration:但这会导致一种非常奇怪的行为.
我有一个带有2个CollectionView控制器的viewcontroller.
我想轮换时只有一个集合视图调整为自定义大小而另一个保持不变.
我试过用:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Adjust cell size for orientation
if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
return CGSizeMake(170.f, 170.f);
}
return CGSizeMake(192.f, 192.f);
}
Run Code Online (Sandbox Code Playgroud)
但是,这只会改变两个集合视图.如何让它仅针对一个集合?
我将水平UICollectionView固定到的所有边缘UITableViewCell。集合视图中的项目是动态调整大小的,我想使表视图的高度等于最高的集合视图单元格的高度。
视图的结构如下:
UITableView
UITableViewCell
UICollectionView
UICollectionViewCell
UICollectionViewCell
UICollectionViewCell
class TableViewCell: UITableViewCell {
private lazy var collectionView: UICollectionView = {
let collectionView = UICollectionView(frame: self.frame, collectionViewLayout: layout)
return collectionView
}()
var layout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 350, height: 20)
layout.scrollDirection = .horizontal
return layout
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let nib = UINib(nibName: String(describing: CollectionViewCell.self), bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: CollectionViewCellModel.identifier)
addSubview(collectionView)
// (using SnapKit)
collectionView.snp.makeConstraints …Run Code Online (Sandbox Code Playgroud)