gpt4 book ai didi

ios - swift 自动布局 : constraints in sub-view not working

转载 作者:行者123 更新时间:2023-11-28 11:02:43 24 4
gpt4 key购买 nike

我正在尝试设置一个 View ,其中包含两个 Collection View 和一个位于中心的 View ,而中心 View 又包含元素。

我一直在尝试使用这样的视觉格式设置约束:

func setupViews() {

self.view.addSubview(playGroundView)
self.view.addSubview(firstCollectionView)
self.view.addSubview(secondCollectionView)

self.playGroundView.backgroundColor = UIColor.clear


aTextView.backgroundColor = UIColor.yellow
titleTextView.backgroundColor = UIColor.green
searchBar?.backgroundColor = UIColor.darkGray


if #available(iOS 9.0, *) {
playGroundView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
firstCollectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
secondCollectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
} else {
// Fallback on earlier versions
}

self.playGroundView.addSubview(publishButton)
self.playGroundView.addSubview(searchBar!)
self.playGroundView.addSubview(aTextView)
self.playGroundView.addSubview(titleTextView)

publishButton.addConstraint(NSLayoutConstraint(item: publishButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))
aTextView.addConstraint(NSLayoutConstraint(item: aTextView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))
titleTextView.addConstraint(NSLayoutConstraint(item: titleTextView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))
searchBar!.addConstraint(NSLayoutConstraint(item: searchBar!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 44))

self.playGroundView.addConstraintsWithFormat("V:[v0]-2-|", views: publishButton)
self.playGroundView.addConstraintsWithFormat("H:[v0]-2-|", views: publishButton)
self.playGroundView.addConstraintsWithFormat("V:|-2-[v0(44)]", views: searchBar!)


self.view.bringSubview(toFront: aTextView)

self.playGroundView.layoutIfNeeded()
self.playGroundView.layoutSubviews()

self.view.addConstraintsWithFormat("H:|-8-[v0(100)][v1][v2(100)]-8-|", views: secondCollectionView,playGroundView,firstCollectionView)

self.view.addConstraintsWithFormat("V:[v0]-2-|", views: playGroundView)
self.view.addConstraintsWithFormat("V:[v0]-2-|", views: secondCollectionView)
self.view.addConstraintsWithFormat("V:[v0]-2-|", views: firstCollectionView)



if(self.isCreate){
self.titleTextView.text = self.recipeDictionary?.value(forKey: "recipeName") as! String!
}
}


// Function to set constraints

func addConstraintsWithFormat(_ format: String, views: UIView...) {

var viewsDictionary = [String: UIView]()
for (index,view) in views.enumerated() {
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}

我没有看到正在渲染的 subview (playGroundView) 内的元素。有人可以建议我在这里做错了什么吗?

最佳答案

我会尝试以下调试:

  1. 临时为 playGroundView 设置纯色背景色而不是 UIColor.clear 并运行它以使 playGroundView 具有正确的大小和位置。
  2. 临时为缺失的 subview 设置边框并运行它以查看 View 是否在 playGroundView 边界内。或者,您也可以运行代码,转到 Xcode > Debug > View Debugging > Capture View Hierarchy
  3. 在添加到 playGroundView 之前,为 publishButtonsearchBar 等设置 translatesAutoresizingMaskIntoConstraints = false。虽然在您的辅助函数 addConstraintsWithFormat 中执行该操作并没有错,但没有必要多次进行该调用。

关于ios - swift 自动布局 : constraints in sub-view not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39939019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com