1

I have a table view controller and the only way for me is to using that so please don't tell me use viewController instead of tableviewController here is my codes for adding a button and this Button is shown in the last cell But I want when user scrolling table view the button be at the bottom of screen without moving

override func viewDidLoad() {
    super.viewDidLoad()


    let submitButton = UIButton(frame: CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50))

    submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
    submitButton.setTitle("Submit", for: .normal)
    submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
    submitButton.titleLabel?.textColor = .white
    submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
    submitButton.layer.cornerRadius = 5
    view.addSubview(submitButton)
Saeed Rahmatolahi
  • 1,317
  • 2
  • 27
  • 60
  • Adding button as footer of the tableview section can be a workaround. Refer this link if you are trying : https://stackoverflow.com/questions/38178509/swift-add-footer-view-in-uitableview – Amit Dec 25 '17 at 10:20
  • this will not work in tableview controller I want the button be I the bottom of screen in any situation but in this link when I run the app I need to scroll table view to see the button – Saeed Rahmatolahi Dec 25 '17 at 10:59
  • Then there can be one approach of adding button on `self.view.window`. But in this approach you have to remove the button when view will disappear. – Amit Dec 25 '17 at 11:06

2 Answers2

1

Try this

declare button as a variable

   var submitButton:UIButton!

and in viewDidLoad

    submitButton = UIButton(frame: CGRect(x: 5, y: UIScreen.main.bounds.size.height - 50 , width: UIScreen.main.bounds.size.width - 10 , height: 50))        
    submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
    submitButton.setTitle("Submit", for: .normal)
    submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
    submitButton.titleLabel?.textColor = .white
    submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
    submitButton.layer.cornerRadius = 5        
    self.view.addSubview(submitButton)

and implement this method

 override func scrollViewDidScroll(_ scrollView: UIScrollView) {

  submitButton.frame = CGRect.init(x: submitButton.frame.origin.x, y:  UIScreen.main.bounds.size.height + scrollView.contentOffset.y - 50 , width: submitButton.frame.width, height: submitButton.frame.height)

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Try this approach if it suits your scenario:

var submitButton = UIButton()

override func viewDidLoad() {
    super.viewDidLoad()


    submitButton.frame = CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50)

    submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
    submitButton.setTitle("Submit", for: .normal)
    submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
    submitButton.titleLabel?.textColor = .white
    submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
    submitButton.layer.cornerRadius = 5
    self.view.window?.addSubview(submitButton)
}

But in this scenario you have to remove the button from superview in viewWillDisappear :

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self. submitButton.removeFromSuperview()
}
Amit
  • 4,837
  • 5
  • 31
  • 46