kishor0011 kishor0011 - 8 days ago 6
Swift Question

How can we add a button over collection view using swift?

I have a

UICollectionViewController
in my
main.storyboard
.

I want to add a circle button to redirect to new page, But in
storyboard
is it possible to drag a button over
UICollectionView
? I don't know about footer how can we achieve.

It should looks like
UICollectionView
is scrolling and button will be over the collection view, I mean no effect on button.

Answer

Yes you can do it,

If using UIViewController Put your button above collectionview like this,

enter image description here

If using UICollectionViewContoller, you can't add button to storyboard, you need to add button programmatically. Call below function in viewDidLoad().

fileprivate func addButton(){
        let button = UIButton()
        button.setTitle("Button", for: UIControlState.normal)
        button.setTitleColor(UIColor.black, for: UIControlState.normal)
        self.view.addSubview(button)
        button.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
        button.center = CGPoint(x: (collectionView?.frame.width)!/2, y: (collectionView?.frame.height)!-30)
}
Comments