【问题标题】:How to change UITableView section borders?如何更改 UITableView 部分边框?
【发布时间】:2017-06-26 15:49:42
【问题描述】:

我有一个 UITableView,我只想为所有分隔符设置相同的宽度。我已经知道一些关于自定义 uitableview 的知识,但这对我来说是个谜。。 基本上,我想将此部分标题分隔符设置为与单元格分隔符相同的宽度,与超级视图具有相同的前导空间。最好的方法是什么?

编辑

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    let header = view as! UITableViewHeaderFooterView
    header.textLabel?.font = header.textLabel?.font.withSize(12)
    header.textLabel?.textColor = ColorConstants.symbolsColor
}

【问题讨论】:

    标签: swift xcode cocoa-touch uikit


    【解决方案1】:

    在表格视图方法 CellForRowAt... 尝试添加此代码

    cell.preservesSuperviewLayoutMargins = false 
    cell.seperatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero
    

    【讨论】:

    • 感谢您的回答,但没有任何改变
    【解决方案2】:

    页眉和页脚没有分隔线,但您可以使用自定义 UIView 模拟它们并将它们放入:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view =  UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
        let px = 1 / UIScreen.main.scale
        let frame = CGRect(x: 0, y: 0, width: self.tableView.frame.size.width, height: px)
        let line = UIView(frame: frame)
        view.addSubview(line)
        line.backgroundColor = self.tableView.separatorColor
        self.tableView.tableHeaderView = view
    }
    

    并将您的其他代码也添加到此函数中,而不是 willDisplayHeaderView。 您可能需要调整 CGRect 尺寸以完全匹配您想要的尺寸。我相信默认的单元格分隔符从 15 点开始(不是 100% 确定)

    感谢:https://stackoverflow.com/a/28177407/7535093

    【讨论】:

    • 我想保持原样插入单元格分隔符。相反,我想更改从边缘到边缘的另一个分隔符。那是部分边界还是smth?
    • 您是在标题中使用自定义视图还是仅在标题中使用?
    • 更新问题
    • 更新了我的答案。希望对你有帮助
    • 它只是在默认标题上方添加了这个自定义视图:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2012-03-08
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多