xcode 在 UICollectionView 中更改滚动方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21095794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change scroll direction in UICollectionView
提问by Recycled Steel
I have a UICollectionView which I have setup, everything works fine (selection, headers, etc), however, I want to change the scroll direction in some situations.
我有一个已设置的 UICollectionView,一切正常(选择、标题等),但是,我想在某些情况下更改滚动方向。
In short if I go into the story board and change the scrollDirection it works fine but I want to do it programatically!
简而言之,如果我进入故事板并更改滚动方向,它可以正常工作,但我想以编程方式进行!
I have tried to change the scroll direction of the collection view directly with something like
我试图用类似的东西直接改变集合视图的滚动方向
[myCollectionView setScrollDirection:....and so on.......
But this does not work, I can not find scrollDirection or similar in there.
但这不起作用,我在那里找不到 scrollDirection 或类似的东西。
I have also tried to setup a flow layout but I am sure i am doing this wrong (i.e. trying to set a ViewLayout to a FlowLayout).
我也尝试过设置流布局,但我确定我做错了(即试图将 ViewLayout 设置为 FlowLayout)。
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[myCollectionView setCollectionViewLayout:flowLayout];
This crashes with a lot of Constraint problems and I suspect I need to do a lot more work with the flowLayout (from what I have found it is a bit above me right now).
这会因很多约束问题而崩溃,我怀疑我需要对 flowLayout 做更多的工作(从我发现它现在有点高于我)。
It should also be noted that I am using a custom cell, headers and footers.
还应该注意的是,我使用的是自定义单元格、页眉和页脚。
In short is there an easy way to do this or not? OR does anyone know a good Flow Layout tutorial?
简而言之,是否有一种简单的方法可以做到这一点?或者有人知道一个好的 Flow Layout 教程吗?
EDIT
编辑
I setup the collection view as such;
我这样设置集合视图;
[myCollectionView setDataSource:self];
[myCollectionView setDelegate:self];
I implement these delegate methods and all work fine
我实现了这些委托方法并且一切正常
viewForSupplementaryElementOfKind
numberOfSectionsInCollectionView
numberOfItemsInSection
cellForItemAtIndexPath
didSelectItemAtIndexPath
I have added the DataSource and Delegate to the .h and also the FlowLayout delegate BUT I am not sure what I should also have for the latter.
我已将 DataSource 和 Delegate 添加到 .h 以及 FlowLayout 委托,但我不确定我还应该为后者添加什么。
Most of the visual layout is done in Story Board, there are a few things such as Font, size and colour which I do programatically.
大多数视觉布局是在 Story Board 中完成的,还有一些东西,例如字体、大小和颜色,我以编程方式完成。
ANOTHER EDIT
另一个编辑
This is the error when I try to change the FlowLayout, I also get this when I try and invalidate the layout.
这是我尝试更改 FlowLayout 时出现的错误,当我尝试使布局无效时也会出现此错误。
Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
无法同时满足约束。可能以下列表中的至少一项约束是您不想要的。试试这个: (1) 查看每个约束并尝试找出您不期望的;(2) 找到添加不需要的约束或约束的代码并修复它。(注意:如果看到 NSAutoresizingMaskLayoutConstraints 不明白,请参考 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)
"<NSAutoresizingMaskLayoutConstraint:0x89967f0 h=--& v=--& V:[menuCell:0x8991700(50)]>",
"<NSLayoutConstraint:0x8991200 menuCell:0x8991700.bottom == UILabel:0x8992be0.bottom + 100>",
"<NSLayoutConstraint:0x898fd50 UILabel:0x8992be0.top == menuCell:0x8991700.top + 3>"
Will attempt to recover by breaking constraint
将尝试通过打破约束来恢复
Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
中断 objc_exception_throw 以在调试器中捕获它。中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。
采纳答案by Matthew Hallatt
Ok, try calling invalidateLayout on your collection view like so:
好的,尝试在您的集合视图上调用 invalidateLayout ,如下所示:
[myCollectionView.collectionViewLayout invalidateLayout];
This forces the collection view to update its layout at that point (See apple documentation here). I'm not certain, but I don't imagine this is called when you change the scroll direction.
这会强制集合视图在此时更新其布局(请参阅此处的苹果文档)。我不确定,但我不认为当你改变滚动方向时会调用它。
See if that gets you anywhere near!
看看这是否能让你靠近!
回答by Pratham Mehta
do this:
做这个:
- (IBAction)changeDirection:(UIButton *)sender
{
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)[self.collectionView collectionViewLayout];
if(layout.scrollDirection == UICollectionViewScrollDirectionHorizontal)
{
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
else
{
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
}
It works for me.
这个对我有用。
回答by prolfe
In swift you can do this:
在 swift 你可以这样做:
//From the collection view subclass
if let layout: UICollectionViewFlowLayout = self.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .Vertical
}
回答by Natasha
You can do it with property observer didSet{}
with your collection view outlet.
您可以使用didSet{}
带有集合视图插座的属性观察器来完成。
Assuming that the outlet for the collectionView is myCollectionView, in the code add a didSet
property observer to the outlet and change the layout's direction. Something like-
假设collectionView的outlet是myCollectionView,在代码中didSet
给outlet添加一个属性观察器,改变布局的方向。就像是-
@IBOutlet weak var myCollectionView:UICollectionView!{
didSet{
let layout = contentCollectionView.collectionViewLayout as!
UICollectionViewFlowLayout
layout.scrollDirection = .Vertical
}
}
Normally a collectionView is dropped in storyboard from object library, it automatically comes with FlowLayout. You can change this layout's flow direction by telling your code that when I get my collection view, I want its layout's scroll direction to be horizontal or vertical.
通常一个 collectionView 是从对象库中放到 storyboard 中的,它会自动附带 FlowLayout。您可以通过告诉代码更改此布局的流动方向,当我获得集合视图时,我希望其布局的滚动方向为水平或垂直。