Tejas Badani
Tejas Badani

Reputation: 68

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

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)

}

Upvotes: 0

Views: 904

Answers (1)

Josh Homann
Josh Homann

Reputation: 16327

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
  2. add padding equal to the pciker height to the bottom of your collectionview with contentInset
  3. scroll the collection view with setContent(offset:animated:) by an amount equal to - the picker height.

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; it really depends on the effect you want to achieve. Note the methods are on UIScrollView which is the parent class of 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. This will automatically push the collectionview up so you don't have to mess withthe constraints.

Upvotes: 1

Related Questions