Home Adjusting button width in UITableViewController (Swift - Xcode)
Reply: 2

Adjusting button width in UITableViewController (Swift - Xcode)

Kinja
1#
Kinja Published in 2018-02-14 07:16:16Z

i would like to know how can i adjust my UIButton to be stationary at the bottom so lets say even if there are a lot of tableview cells. The button will still stay put in the bottom.

Arun Kumar
2#
Arun Kumar Reply to 2018-02-14 09:47:55Z

Actually there are 3 ways to achieve that in my knowledge.

1 -> By Using UIViewController instead of UITableViewController

In UIViewController you can add UITableView , then add UIButton at the bottom of the UIViewController as per your requirement.

2 -> Add Button on the UINavigationController View

You can add a button on UINavigationController's view (if you are using UINavigationController) programmatically.

3 -> Add Button on the Application Window

You can add a button on UIWindow programmatically.

For 2nd & 3rd point

These cases are applicable when you want to use only UITableViewController. For this you need to create a button programmatically in your TableViewController.

 let button : UIButton = {
    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Redeem", for: .normal)
    button.backgroundColor = .green
    return button
}()

Then add the button either on UIWindow or on UINavigationController's view , like this:

If you want to add button UIWindow

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if let window = UIApplication.shared.keyWindow
    {
        window.addSubview(button)

        button.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
      let bottomSpaceConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:[v0(40)]-20-|", options: .init(rawValue: 0), metrics: nil, views: ["v0" : button])
        NSLayoutConstraint.activate(bottomSpaceConstraints)
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
    }
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    button.removeFromSuperview()
}

and if you want to add it in UINavigationController.

   override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if let navigationView = self.navigationController?.view
    {
        navigationView.addSubview(button)
        button.centerXAnchor.constraint(equalTo: navigationView.centerXAnchor).isActive = true
      let bottomSpaceConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:[v0(40)]-20-|", options: .init(rawValue: 0), metrics: nil, views: ["v0" : button])
        NSLayoutConstraint.activate(bottomSpaceConstraints)
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
    }
}

Remember In both cases 2nd & 3rd, You should remove that button in viewWillDisappear method, cause you don't want that button appear on every screen.

In All cases result would be something like this:

Rocky
3#
Rocky Reply to 2018-02-14 08:34:02Z

Ok, to get a FIXED button on bottom on as you required follow the below steps:

  1. Add a UIViewController on your Storyboard
  2. Add a UITableView on UIViewController
  3. Add a UIView at bottom of UIViewController with Fixed Height
  4. Add Constraints to UIView for bottom, left, right to its super view & Make UIView.height Constraints a fix value
  5. Add Constraints to UITableView for top, left, right to its superview & bottom with UIView
  6. Add a UIButton on UIView with Horizontally & Vertically centre to its superview

for your reference demo project zip also added with these steps

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311511 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO