简体   繁体   English

如何在 Swift 中为 UITableView 设置左右边距

[英]How to set left and right margin for UITableView in Swift

I am using UITableView inside my controller that displays cells with some informations (title, subtitle and image).我在我的 controller 中使用UITableView ,它显示带有一些信息(标题、副标题和图像)的单元格。 I would like to add some padding to the tableView , but I can't find the right solution.我想为tableView添加一些填充,但我找不到正确的解决方案。 This is my code for table view:这是我的表格视图代码:

private let tableView: UITableView = {
    let tableView = UITableView(frame: .zero, style: .grouped)
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.backgroundColor = .clear
    tableView.separatorStyle = .none
    tableView.allowsSelection = false
    tableView.showsVerticalScrollIndicator = false
    tableView.estimatedRowHeight = 44.0
    tableView.rowHeight = UITableView.automaticDimension
    return tableView
}()

Inside viewDidLoad() I setup constraints for that tableViewviewDidLoad()里面我为那个tableView设置了约束

NSLayoutConstraint.activate([
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    tableView.topAnchor.constraint(equalTo: view.topAnchor),
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

Right now my view looks like this:现在我的观点是这样的:

在此处输入图像描述

Now when I try to configure contentInset property inside my tableView closure by adding this:现在,当我尝试通过添加以下内容在tableView闭包中配置contentInset属性时:

tableView.contentInset = .init(top: 0, left: 23.5, bottom: 0, right: -23.5)

As you can see it only added space on the left side, but looking at the debug view hierarchy I can say that it moved that contentView to the right side (It's still have the same width as its superview)如您所见,它仅在左侧增加了空间,但是查看调试视图层次结构,我可以说它将该contentView移到了右侧(它仍然具有与其父视图相同的宽度) 在此处输入图像描述

在此处输入图像描述

Info from view debugger.来自视图调试器的信息。 TableView :表视图

<UITableView: 0x7fba82016000; frame = (0 0; 390 844); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x60000023de90>; layer = <CALayer: 0x600000c4b9e0>; contentOffset: {-23.333333333333332, -47}; contentSize: {390, 635.66666666666663}; adjustedContentInset: {47, 23.5, 34, -23.5}; dataSource: <Myapp.WelcomeViewController: 0x7fba81c08f60>>

and First cell:和第一个单元格:

<UITableViewCellContentView: 0x7fba84021250; frame = (0 0; 390 87); gestureRecognizers = <NSArray: 0x6000002cf900>; layer = <CALayer: 0x600000c2b900>>

TableView has the same width as cell (390 vs 390). TableView与单元格具有相同的宽度(390 对 390)。 How can I add my paddings to the left and right to the tableView , without making constraints on that tableView (I want that area to also be scrollable)如何在tableView的左侧和右侧添加我的填充,而不对该tableView进行限制(我希望该区域也可以滚动)

You can do this either by setting the .layoutMargins on the cell's contentView (either in the cell class itself or in cellForRowAt :您可以通过在单元格的contentView上设置.layoutMargins来执行此操作(在单元格 class 本身或在cellForRowAt中:

cell.contentView.layoutMargins = .init(top: 0.0, left: 23.5, bottom: 0.0, right: 23.5)

or on the table view itself:或在表格视图本身上:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.layoutMargins = .init(top: 0.0, left: 23.5, bottom: 0.0, right: 23.5)
    // if you want the separator lines to follow the content width
    tableView.separatorInset = tableView.layoutMargins
}

You may try this:你可以试试这个:

Add the constant while adding the constraints在添加约束的同时添加常量

NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
   tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 20),
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

Try this尝试这个

override var frame: CGRect {
        get {
            return super.frame
        }
        set (newFrame) {
            var frame = newFrame
            frame.origin.x += 10
            frame.size.width -= 2 * 10
            super.frame = frame
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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