Xcode:AccessoryType.checkmark 将其他视图推向左侧 [英] Xcode: AccessoryType.checkmark pushes other views to the left

查看:24
本文介绍了Xcode:AccessoryType.checkmark 将其他视图推向左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 10、Swift 5、iOS 12

Xcode 10, Swift 5, iOS 12

我的应用包含 3 个 ViewControllers:

My app contains 3 ViewControllers:

  1. 登录
  2. 表视图
  3. TableView 中单行数据的详细信息

如果在 ViewController 3 中编辑并保存数据并使用返回"按钮(通过一个 NavigationController)返回到 ViewController 2,则在 TableView 中的相应行添加一个复选标记.

If you edit and save the data in ViewController 3 and use the "back" button (through a NavigationController) to go back to ViewController 2, a checkmark is added to the corresponding row in the TableView.

问题是这个复选标记将其他所有东西都向左推,即使我为它留了一些空间:

The problem is that this checkmark pushes everything else to the left, even though I left some space for it:

设置:

  • 水平 StackView 有 2 个子视图,每个子视图有 1 个标签
  • StackView 设置为填充"
  • 第二个子视图的宽度是第一个的一半
  • 左边 10 分,右边 40 分(也已经用 100 分进行了测试,但没有变化)
  • Horizontal StackView with 2 subviews with 1 label each
  • The StackView is set to "Fill"
  • The second subview is half as wide as the first one
  • 10 pts leading on the left and 40pts trailing on the right (also already tested with 100pts but no change)

我已经尝试向每一行添加一个 AccessoryType.none(如此处的建议)当我创建它们时 - 像这样:

I already tried adding an AccessoryType.none to each row (as suggested here) when I create them - like this:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.setLabels("Label1","Label2")
    tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none
    //cell.accessoryType = UITableViewCell.AccessoryType.none //This doesn't work either

    return cell
}

但它没有被添加(与其他类型相同).

but it doesn't get added (same thing with the other types).

我该如何解决这个问题?是否可以在仍然使用 AccessoryType 的同时在其他所有内容后面"设置复选标记?

How do I fix this? Is it possible to set the checkmark "behind" everything else while still using an AccessoryType?

推荐答案

在您创建的用于处理单元格的 UITableViewCell 中添加以下函数.此函数测试当前单元格是否设置了附件类型,如果没有,则使单元格小 40pts,从而使具有和不具有附件的单元格具有相同的自动布局大小.

Inside your UITableViewCell you created to handle the cell add the following function. This function tests if this current cell has an accessoryType set and if not then it makes the cell 40pts smaller, thus making the cells with and without accessory the same size for the auto layout.

override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame = newFrame
        if self.accessoryType == .none {
            frame.size.width -= 40
        }
        super.frame = frame
    }
}

这篇关于Xcode:AccessoryType.checkmark 将其他视图推向左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆