[英]Adjusting button width in UITableViewController (Swift - Xcode)
Actually there are 3 ways to achieve that in my knowledge.实际上,据我所知,有 3 种方法可以实现这一目标。
1 -> By Using UIViewController
instead of UITableViewController
1 -> 使用
UIViewController
而不是UITableViewController
In UIViewController
you can add UITableView
, then add UIButton
at the bottom of the UIViewController
as per your requirement.在
UIViewController
您可以添加UITableView
,然后根据您的要求在UIViewController
底部添加UIButton
。
2 -> Add Button on the UINavigationController View
2 -> 在
UINavigationController View
上添加按钮
You can add a button on UINavigationController's view
(if you are using UINavigationController
) programmatically.您可以以编程方式在
UINavigationController's view
上添加一个按钮(如果您使用UINavigationController
)。
3 -> Add Button on the Application Window
3 -> 在
Application Window
上添加按钮
You can add a button on UIWindow
programmatically.您可以以编程方式在
UIWindow
上添加按钮。
For 2nd & 3rd point对于第 2 点和第 3 点
These cases are applicable when you want to use only UITableViewController
.当您只想使用
UITableViewController
时,这些情况适用。 For this you need to create a button programmatically in your TableViewController
.为此,您需要在
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:然后在
UIWindow
或UINavigationController's view
上添加按钮,如下所示:
If you want to add button UIWindow
如果要添加按钮
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
.如果你想在
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.请记住,在第 2 种和第 3 种情况下,您应该在
viewWillDisappear
方法中删除该按钮,因为您不希望该按钮出现在每个屏幕上。
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:好的,要根据需要在底部获得 FIXED 按钮,请按照以下步骤操作:
UIViewController
on your Storyboard
Storyboard
UIViewController
添加一个UIViewController
UITableView
on UIViewController
UIViewController
上添加一个UITableView
UIView
at bottom of UIViewController
with Fixed HeightUIViewController
底部添加一个UIView
固定高度UIView
for bottom, left, right to its super view & Make UIView.height
Constraints
a fix valueUIView
的底部、左侧、右侧到其超级视图UIView.height
Constraints
成为固定值Constraints
to UITableView
for top, left, right to its superview & bottom with UIView
UIView
将Constraints
添加到UITableView
的顶部、左侧、右侧到其超级视图和底部UIButton
on UIView
with Horizontally & Vertically centre to its superviewUIView
以水平和垂直居中添加一个UIButton
到其超级视图for your reference demo project zip also added with these steps为您的参考演示项目 zip也添加了这些步骤
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.