0
두 개의 컬렉션 뷰와 가운데에 뷰가있는 뷰를 설정하려고합니다. 뷰가 가운데에 요소가 있습니다. 스위프트 자동 레이아웃 : 하위보기의 제약 조건이 작동하지 않습니다.
은이 같은 시각적 형식을 사용하여 제약 조건을 설정하려고되었습니다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))
}
내가 렌더링되는 서브 뷰 내부의 요소 (playGroundView)를 참조하지 않는다. 누군가 내가 여기서 잘못하고있는 것을 제안 할 수 있습니까?
놀이터보기의 배경색을 설정해 보았습니다. 보기의 프레임은 예상대로입니다. 포함 된 뷰 요소가 표시되지 않습니다. 코드에서 볼 수 있듯이 해당 뷰의 배경을 설정하려고했습니다. – userx
subview에 직접 설정하는 대신 playGroundView에 제약 조건을 추가 할 수도 있습니다. 'self.playGroundView.addConstraint (NSLayoutConstraint (item : publishButton, attribute : .height, relatedBy :. equal, toItem : nil, 속성 : .notAnAttribute, 배율 : 1, 상수 : 44)) ' –
하위 뷰를 의심하고 있습니다. '프레임이 0이므로 배경색조차도 표시되지 않습니다. –