简体   繁体   中英

Adjusting button width in UITableViewController (Swift - Xcode)

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.

在此处输入图片说明

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:

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM