Its a bit late, but may be it can help someone. You can do it very easily via programatically. Just create an instance of the button, set the desired property of button It is preferable to use constraints over frame cause it'll render the button at same position on every screen size. I am posting the code which will make the button round. Play with the value of constraints to change the position and size of button.
Following the answer of @Kunal Kumar, it works on the UIScrollView, and UIView, but it doesn't works on the UITableView. I found it is easy to make it works with UITableView, only need to add one line code. Thank you so much @Kunal Kumar
in the viewDidLoad, change self.view.addSubview(roundButton) to
self.navigationController?.view.addSubview(roundButton) and it will work!!
by the way, I think we need to add super.viewWillLayoutSubviews() in the viewWillLayoutSubviews() method.
below is the all code with above mentioned,
Swift 3
// MARK: Floating Button
var roundButton = UIButton()
func createFloatingButton() {
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.addTarget(self, action: #selector(ButtonClick(_:)), for: UIControlEvents.touchUpInside)
//change view to navigationController?.view, if you have a navigationController in this tableview
self.navigationController?.view.addSubview(roundButton)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
roundButton.backgroundColor = UIColor.lightGray
roundButton.clipsToBounds = true
roundButton.setImage(UIImage(named:"ic_wb_sunny_48pt"), for: .normal)
roundButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
roundButton.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -3),
roundButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -53),
roundButton.widthAnchor.constraint(equalToConstant: 50),
roundButton.heightAnchor.constraint(equalToConstant: 50)])
}
It's simply because your UIButton is under the UITableView, move it in the storyboard's tree so it appears like this :
Its a bit late, but may be it can help someone. You can do it very easily via programatically. Just create an instance of the button, set the desired property of button It is preferable to
use constraints over frame
cause it'll render the button at same position on every screen size. I am posting the code which will make the button round. Play with the value of constraints to change the position and size of button.Swift 3
This button code below:
Swift 4.2: COMPLETE VIEW CONTROLLER IMPLEMENTATION
Following the answer of @Kunal Kumar, it works on the
UIScrollView
, andUIView
, but it doesn't works on theUITableView
. I found it is easy to make it works withUITableView
, only need to add one line code. Thank you so much @Kunal Kumarin the
viewDidLoad
, changeself.view.addSubview(roundButton)
toself.navigationController?.view.addSubview(roundButton)
and it will work!!by the way, I think we need to add
super.viewWillLayoutSubviews()
in theviewWillLayoutSubviews()
method.below is the all code with above mentioned,
Swift 3
Put a table view inside a view controller. You should then be able to add the the button on top of the table view.