[英]Scroll UICollectionViewCell Swift 3 acording to the selected UITextField
I created a storyboard with a toolbar. 我用工具栏创建了一个情节提要。 Also, I created a UICollectionViewCell/XIB and imported it inside the storyboard.
另外,我创建了一个UICollectionViewCell / XIB并将其导入情节提要中。 I added many UITextFields inside the UICollectionViewCell.
我在UICollectionViewCell内添加了许多UITextField。 When I select the UITextFields which are in the bottom of the screen, the screen doesn't scroll to show it.
当我选择屏幕底部的UITextFields时,屏幕不会滚动以显示它。 Could you give me some tip about how to solve that?
您能给我一些解决方法的建议吗?
Is your cell height dynamic or fixed? 您的像元高度是动态的还是固定的? If it is fixed, you can have a trick like, once the textfield becomes active, detect the selected cell with some
delegate
or didSelectItemAt
, then multiply the indexPath.row of the cell with cellHeight property, then set contentOffset
of collectionView
to that value like: 如果它是固定的,则可以使用技巧,一旦文本字段变为活动状态,使用某些
delegate
或didSelectItemAt
检测选定的单元格,然后将单元格的indexPath.row与cellHeight属性相乘,然后将collectionView
contentOffset
设置为该值,例如:
// cellHeight property is something you should have declared somewhere.
collectionView.setContentOffset(CGPoint(x: 0.0, y: cellHeight * indexPath.row), animated: true)
If the solution above does not work, then get the keyboardHeight programmatically, or set a value that would be good enough for you to scroll up, for example I will set it to 200, then get the currentOffset, add your value to currentOffset's y value in didSelectItemAt
method, (just for reference): 如果上述解决方案不起作用,请以编程方式获取keyboardHeight,或者设置一个足以使您向上滚动的值,例如,我将其设置为200,然后获取currentOffset,将您的值添加到currentOffset的y值中在
didSelectItemAt
方法中,(仅供参考):
let currentOffSet = collectionView.contentOffset
let newOffset = CGPoint(x: 0.0, y: currentOffSet.y + 200.0)
collectionView.setContentOffset(newOffset, animated: true)
This might be a better solution depending on your application. 根据您的应用程序,这可能是更好的解决方案。 But remember to set offSet back to original value, maybe in
resignFirstResponder
kind of method with your textField, so there wont be a weird behaviour each time user taps a cell. 但是请记住将offSet设置回原始值,也许使用textField的
resignFirstResponder
方法,所以每次用户点击一个单元格时都不会出现怪异的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.