0

I would like to place my collectionView right underneath my Segmented Control, however I dont know if I am getting my constraints wrong. I would like to also place my segmented control to fill the whole frame width minus ten but centered above the collectionView.

Heres the code I have been trying:

    let item = ["Past", "Future"]
    let customSC = UISegmentedControl(items: item)

    customSC.selectedSegmentIndex = 0
    let frame = UIScreen.mainScreen().bounds
    customSC.frame = CGRectMake(0, 0,
                                frame.width, 25)

    customSC.layer.cornerRadius = 5.0  // Don't let background bleed
    customSC.backgroundColor = MaterialColor.white
    customSC.tintColor = MaterialColor.red.accent3

    customSC.translatesAutoresizingMaskIntoConstraints = false
    //customSC.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor)
    //collectionView?.translatesAutoresizingMaskIntoConstraints = false
    collectionView?.addSubview(customSC)
    customSC.centerXAnchor.constraintEqualToAnchor(collectionView?.centerXAnchor).active = true
    //collectionView?.topAnchor.constraintEqualToAnchor(customSC.bottomAnchor,constant: 2).active = true

Here is the image of my output. I would my Segmented View to be my first view to fill the whole width frame and my collection view to show right underneath my Segmented control.

enter image description here

Thanks.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Aboogie
  • 450
  • 2
  • 9
  • 27

1 Answers1

0

You need to add CustomSc on current View not on Collection view .do something like this

    self.view.addSubview(customSC)

set CollectionView frame like this

    collectionView.frame = CGRectMake(0, customSC.frame.origin.y + 25, self.view.frame.size.width, set Acc. to your req.)

Now add Collectionview on Current View like this

    self.view.addSubview(collectionView)
Sumit Jangra
  • 651
  • 5
  • 16