简体   繁体   English

当单元格被DatePicker隐藏时移动滚动视图

[英]Move scroll view when cell hidden by the DatePicker

I have a collection view in which if I click an element a date picker shows up. 我有一个集合视图,如果单击某个元素,则会显示一个日期选择器。 Now, I have been trying to scroll my view up if it gets hidden by the date picker but with no luck. 现在,我一直在尝试向上滚动我的视图(如果它被日期选择器隐藏但没有运气)。 Any help would be appreciated. 任何帮助,将不胜感激。 This is my code for creating a DatePicker with a toolbar 这是我用工具栏创建DatePicker的代码

func createUIPickerView(){

    //:Creating a date picker and the toolbar
    picker = UIDatePicker(frame: CGRect(x: 0, y: self.view.frame.size.height-216, width: self.view.frame.size.width, height: 216))
    picker.backgroundColor = UIColor.white
    picker.datePickerMode = .time
    toolBar = UIToolbar(frame: CGRect(x: 0, y: self.view.frame.size.height - 50 - 216, width: self.view.frame.size.width, height: 50))
    toolBar.barStyle = .default
    toolBar.barTintColor = UIColor.white
    toolBar.isTranslucent = true
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(CalenderVC.doneClick))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CalenderVC.cancelClick))
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    self.view.addSubview(toolBar)
    self.view.addSubview(picker)

}

I can't see code for your collection view, but either you: 我看不到您的收藏夹视图的代码,但是您要么:

  1. change the bottom constraint on the collectionview to have a constant equal to the picker height, then call setNeedsLayout or layoutIfNeeded to move it 将collectionview的底部约束更改为一个常数,使其等于拾取器高度,然后调用setNeedsLayout或layoutIfNeeded将其移动
  2. add padding equal to the pciker height to the bottom of your collectionview with contentInset 添加填充等于pciker高度与您的CollectionView的底部contentInset
  3. scroll the collection view with setContent(offset:animated:) by an amount equal to - the picker height. 使用setContent(offset:animated :)滚动收集视图等于-拾取器高度的量。

I usually vote for 1, but possibly a combination of 2 and 3, or maybe just 2 if you know you don't need the inset; 我通常投1票,但也可能投2票和3票,或者如果您知道不需要插入票,则投2票。 it really depends on the effect you want to achieve. 这实际上取决于您要实现的效果。 Note the methods are on UIScrollView which is the parent class of UICollectionView. 注意,方法在UIScrollView上,UIScrollView是UICollectionView的父类。

EDIT: Oh there is one more you can do since you are not using the datePicker as an inputView to a textField: embed your collection view in a stackview, then add the picker to the stackview's arranged subviews. 编辑:哦,您还可以做更多的事情,因为您没有将datePicker用作textField的inputView:将您的集合视图嵌入stackview中,然后将选择器添加到stackview的已安排子视图中。 This will automatically push the collectionview up so you don't have to mess withthe constraints. 这将自动将collectionview向上推,因此您不必弄乱约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM