简体   繁体   English

根据选定的UITextField滚动UICollectionViewCell Swift 3

[英]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: 如果它是固定的,则可以使用技巧,一旦文本字段变为活动状态,使用某些delegatedidSelectItemAt检测选定的单元格,然后将单元格的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.

相关问题 UIcollectionViewCell中的UITextField的单个边框(swift 3 xcode) - single border for UITextField within a UIcollectionViewCell (swift 3 xcode) 有什么方法可以滚动到集合中选定的UICollectionViewCell吗? - Is there any way to scroll to the selected UICollectionViewCell in a collection? UICollectionViewCell 在我滚动时被选中 - UICollectionViewCell gets selected when i scroll 如何在UIView中显示所选UICollectioNViewCell的图像? (快速3) - How to display image of selected UICollectioNViewCell in UIView? (Swift 3) 将UITextField保存到UICollectionViewCell中 - Save a UITextField into a UICollectionViewCell (Swift) 将 UIDatePicker 选择的日期分配给 UITextField - (Swift) Assign UIDatePicker selected date to UITextField 尝试在UICollectionViewCell Swift中覆盖“selected”以获取自定义选择状态 - Trying to override “selected” in UICollectionViewCell Swift for custom selection state UICollectionViewCell的UiTextField,在UICollectionViewController中重叠 - UICollectionViewCell's UiTextField, overlapping in UICollectionViewController 关闭UICollectionViewCell内的UITextField的键盘 - Dismiss keyboard for UITextField that is inside of a UICollectionViewCell 在Swift4上被键盘隐藏时,如何滚动UITextField? - How to Scroll UITextField when hides by Keyboard on Swift4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM