catcatcat
catcatcat

Reputation: 377

Swift 3 - Floating Button over UICollectionView

I created a floating button by this first answer. It works but when the UICollectionViewlaunched, the floating button still square and became circle after all datas appear (after loadAPI finished running).

Here's my code:

 override func viewDidLoad() {
    super.viewDidLoad()

    self.roundButton = UIButton(type: .custom)
    self.roundButton.setTitleColor(UIColor.orange, for: .normal)
    self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
    self.view.addSubview(self.roundButton)

    self.loadAPI(Page: 1)
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
    roundButton.backgroundColor = green
    roundButton.clipsToBounds = true
    roundButton.setImage(UIImage(named:"ic_add_white_2x"), for: .normal)
    roundButton.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        roundButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
        roundButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20),
        roundButton.widthAnchor.constraint(equalToConstant: 60),
        roundButton.heightAnchor.constraint(equalToConstant: 60)
    ])
}

@IBAction func ButtonClick(_ sender: UIButton){
 //print("clicked")
}

I need the button being a circle since the UICollectionView first appears. Can somebody help me please? Thanks!

Upvotes: 1

Views: 1445

Answers (1)

Ravi Padsala
Ravi Padsala

Reputation: 126

// update this code

override func viewDidLoad() {
    super.viewDidLoad()

    self.roundButton = UIButton(type: .custom)
    self.roundButton.setTitleColor(UIColor.orange, for: .normal)
    self.roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
    self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
    self.view.addSubview(self.roundButton)

    self.loadAPI(Page: 1)
}

Upvotes: 2

Related Questions