【问题标题】:Section Index Titles Cover UITableViewCell View部分索引标题覆盖 UITableViewCell 视图
【发布时间】:2020-03-23 20:59:19
【问题描述】:

我正在尝试以编程方式将堆栈视图添加到我的 UITableViewCell。

部分索引标题会阻碍视图。

这是我用来在 UITableViewCell 中嵌入堆栈视图的代码。

self.addSubview(mainStack)
mainStack.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: self.TOP_PADDING).isActive = true
mainStack.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: -self.BOTTOM_PADDING).isActive = true
mainStack.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: self.SIDE_PADDING).isActive = true
mainStack.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true

如何在不依赖固定常量(即一些固定填充)的情况下修复视图障碍?

【问题讨论】:

    标签: ios swift uitableview uistackview ios-autolayout


    【解决方案1】:

    改变

    mainStack.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
    

    mainStack.trailingAnchor.constraint(equalTo: self.contentView.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
    

    结果:

    【讨论】:

    • 除了约束之外,您应该将单元格的子视图添加到contentView,而不是直接添加到单元格本身。
    • @dan 谢谢你的评论。直觉上这是有道理的(给定名称“contentView”),但你有这个来源吗?除了这个,我没有遇到任何添加到单元格本身的问题。
    • @JoshWolff - 查看 Apple 文档的这一页:developer.apple.com/documentation/uikit/uitableviewcell - 图片清楚地展示了为什么应将元素添加到 contentView 而不是单元格本身。
    • @DonMag 啊,明白了。谢谢!
    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 2011-12-23
    • 2017-01-03
    • 1970-01-01
    • 2015-04-22
    相关资源
    最近更新 更多