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:
UIViewController
on your Storyboard
UITableView
on UIViewController
UIView
at bottom of UIViewController
with Fixed HeightUIView
for bottom, left, right to its super view & Make UIView.height
Constraints
a fix valueConstraints
to UITableView
for top, left, right to its superview & bottom with UIView
UIButton
on UIView
with Horizontally & Vertically centre to its superviewfor 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.