简体   繁体   English

在 UITableViewController (Swift - Xcode) 中调整按钮宽度

[英]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.我想知道如何将 UIButton 调整为在底部固定,因此即使有很多 tableview 单元格也可以说。 The button will still stay put in the bottom.该按钮仍将保持在底部。

在此处输入图片说明

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:然后在UIWindowUINavigationController'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 按钮,请按照以下步骤操作:

  1. Add a UIViewController on your Storyboard在你的Storyboard UIViewController添加一个UIViewController
  2. Add a UITableView on UIViewControllerUIViewController上添加一个UITableView
  3. Add a UIView at bottom of UIViewController with Fixed HeightUIViewController底部添加一个UIView固定高度
  4. Add Constraints to UIView for bottom, left, right to its super view & Make UIView.height Constraints a fix value将约束添加到UIView的底部、左侧、右侧到其超级视图UIView.height Constraints成为固定值
  5. Add Constraints to UITableView for top, left, right to its superview & bottom with UIView使用UIViewConstraints添加到UITableView的顶部、左侧、右侧到其超级视图和底部
  6. Add a 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.

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