【问题标题】:Fill super view using layoutAnchor使用 layoutAnchor 填充超级视图
【发布时间】:2017-07-09 09:31:06
【问题描述】:

我有一个奇怪的问题,因为我无法使用布局锚填充整个超级视图区域

这是我自定义 UIView 类的代码:

   private func setupTableView() {
            addSubview(tableView)
            tableView.translatesAutoresizingMaskIntoConstraints = false

            let margins = layoutMarginsGuide

            let trailingAnchor = tableView.trailingAnchor.constraint(equalTo: margins.trailingAnchor)
            let leadingAnchor = tableView.leadingAnchor.constraint(equalTo: margins.leadingAnchor)
            let topAnchor = tableView.topAnchor.constraint(equalTo: margins.topAnchor)
            let bottomAnchor = tableView.bottomAnchor.constraint(equalTo: margins.bottomAnchor)

            NSLayoutConstraint.activate([trailingAnchor, leadingAnchor, topAnchor, bottomAnchor])
}

结果我在左右站点上得到了奇怪的边距:

【问题讨论】:

    标签: swift uiview autolayout ios-autolayout


    【解决方案1】:

    您正在相对于layoutMarginsGuide 的边距布置tableView。如果你想让它走到你的UIView 的边缘,那么你需要使用UIView 的锚点:

    private func setupTableView() {
        addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false
    
        let trailingAnchor = tableView.trailingAnchor.constraint(equalTo: trailingAnchor)
        let leadingAnchor = tableView.leadingAnchor.constraint(equalTo: leadingAnchor)
        let topAnchor = tableView.topAnchor.constraint(equalTo: topAnchor)
        let bottomAnchor = tableView.bottomAnchor.constraint(equalTo: bottomAnchor)
    
        NSLayoutConstraint.activate([trailingAnchor, leadingAnchor, topAnchor, bottomAnchor])
    }
    

    【讨论】:

    • 我们都去过那里。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多