安全区域布局指南在UITableView的BackgroundView上无效

4
我遇到的问题是标签被固定在屏幕底部,而应该固定在“安全区域布局指南”,这样可以将标签放置在iPhone线上方。
以下是代码...
class CustomTableViewController: UITableViewController {

   override func viewDidLoad() {
       super.viewDidLoad()

       tableView.tableFooterView = UIView(frame: .zero)
       tableView.backgroundView = CustomBackgroundView()
   } 

}

.

class CustomBackgroundView: UIView {

   override init(frame: CGRect) {
       super.init(frame: frame)
       setupSubviews()
   }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }

   private func setupSubviews() {
       let label = UILabel()
       label.text = "Hello, World!"
       label.textAlignment = .center
       label.translatesAutoresizingMaskIntoConstraints = false

       addSubview(label)

       label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
       label.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor).isActive = true
   } 

}

enter image description here


你有什么问题? - Ammar
我已经更新了帖子。请查看。 - dispatchswift
2个回答

0
您看到这种行为是因为每个UIView都有自己的SafeAreaLayoutGuide。默认情况下,通用UIView子类的SafeAreaLayoutGuide不包括您要查找的安全区域。您必须使用表视图的SafeAreaLayoutGuide
您可以像这样做:
class CustomBackgroundView: UIView {

    var safetyAreaBottomAnchor: NSLayoutYAxisAnchor? {
        didSet {
            guard let safetyAreaBottomAnchor = safetyAreaBottomAnchor else { return }
            label.bottomAnchor.constraint(equalTo: safetyAreaBottomAnchor).isActive = true
        }
    }

    private let label = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupSubviews() {
        label.text = "Hello, World!"
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false

        addSubview(label)

        label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    } 
}

然后在你的UITableViewController中进行如下操作:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let customBackgroundView = CustomBackgroundView()
    tableView.tableFooterView = UIView(frame: .zero)
    tableView.backgroundView = customBackgroundView

    customBackgroundView.safetyAreaBottomAnchor = tableView.safeAreaLayoutGuide.bottomAnchor
}

0

对于所有这些年来从Google到达的人,为了使其工作,我不得不从我的自定义视图中返回父视图的safeAreaLayoutGuide。请参见下文:

class CustomBackgroundView: UIView {
    override var safeAreaLayoutGuide: UILayoutGuide {
        guard let superview = superview else { return UILayoutGuide() }
        return superview.safeAreaLayoutGuide
    }
}

这使得所有的限制条件都按预期工作。确保在添加背景视图之前不要使用safeAreaLayoutGuide

func setupBackgroundViews()
{
    tableView.backgroundView = customBackgroundView
    anotherView.topAnchor.constraint(equalTo: customBackgroundView.safeAreaLayoutGuide.topAnchor).isActive = true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接