以编程方式将按钮与 UITableView 单元格的左侧对齐 Swift

KLM*_*KLM 1 xcode uitableview ios uicollectionview swift

我正在使用 Swift 5、Xcode 11 和 ui 表视图控制器创建一个简单的应用程序。在 ui 表视图中,我想要 2 个按钮:一个按钮在我的表视图左侧,另一个在右侧。我尝试了许多其他相关/类似问题的答案,但都失败了(可能是因为 1. 太旧了,2. 用 OBJ-C 编写的答案)。

这是我的表视图控制器:


import UIKit

@objcMembers class CustomViewController: UITableViewController {

    var tag = 0

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    // 3
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        tag = tag + 1

        let cell = tableView.dequeueReusableCell(withIdentifier: "themeCell", for: indexPath) as! ThemeCell
        var cellButton: UIButton!


        cellButton = UIButton(frame: CGRect(x: 5, y: 5, width: 50, height: 30))
        cell.addSubview(cellButton)
        cell.img.image = UIImage(named: SingletonViewController.themes[indexPath.row])
        cell.accessoryView = cellButton
        cellButton.backgroundColor = UIColor.red
        cellButton.tag = tag
        return cell
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我目前得到的: IMG

Ruc*_*dia 6

添加以下代码以应用约束有效

let cellButton = UIButton(frame: CGRect.zero)
cellButton.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(cellButton)
cell.accessoryView = cellButton
cellButton.backgroundColor = UIColor.red

cellButton.leadingAnchor.constraint(equalTo: cell.leadingAnchor, constant: 5).isActive = true
cellButton.topAnchor.constraint(equalTo: cell.topAnchor, constant: 5).isActive = true
cellButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
cellButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
Run Code Online (Sandbox Code Playgroud)


use*_*632 5

您可以从约束中实现左右对齐按钮。下面是我的代码,用于向右或向左对齐视图。

override func viewDidLoad() {

        let leftButton = UIButton(type: .custom)
        leftButton.backgroundColor = UIColor.red
        self.view.addSubview(leftButton)
        
        leftButton.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraint = leftButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20)
        let verticalConstraint = leftButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        let widthConstraint = leftButton.widthAnchor.constraint(equalToConstant: 100)
        let heightConstraint = leftButton.heightAnchor.constraint(equalToConstant: 100)
        NSLayoutConstraint.activate([horizontalConstraint, verticalConstraint, widthConstraint, heightConstraint])
        
        let rightButton = UIButton(type: .custom)
        rightButton.backgroundColor = UIColor.red
        self.view.addSubview(rightButton)
        
        rightButton.translatesAutoresizingMaskIntoConstraints = false
        let horizontalConstraintRight = rightButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
        let verticalConstraintRight = rightButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        let widthConstraintRight = rightButton.widthAnchor.constraint(equalToConstant: 100)
        let heightConstraintRight = rightButton.heightAnchor.constraint(equalToConstant: 100)
        NSLayoutConstraint.activate([horizontalConstraintRight, verticalConstraintRight, widthConstraintRight, heightConstraintRight])
        
        
    }
Run Code Online (Sandbox Code Playgroud)

[![左右对齐视图约束][1]][1] [1]:https://i.stack.imgur.com/tMJ8N.jpg