2016-10-09 3 views
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)를 참조하지 않는다. 누군가 내가 여기서 잘못하고있는 것을 제안 할 수 있습니까?

답변

3

내가 디버그에 다음을 시도 할 것입니다 :

  1. 일시적으로 대신 UIColor.clear의 playGroundView하고 playGroundView가 정확한 크기와 위치를 가지고있다 할 실행 단색 배경 색상을 설정합니다.
  2. 일시적으로 누락 된 하위 뷰에 테두리를 설정하고 실행하여보기가 playGroundView 범위 내에 있는지 여부를 확인합니다. 또는 코드를 실행하여 Xcode> 디버그> 디버깅보기> 캡처 뷰 계층 구조로 이동하십시오.
  3. playGroundView에 추가하기 전에 publishButton, searchBar 등의 translatesAutoresizingMaskIntoConstraints = false를 설정하십시오. 도우미 기능 addConstraintsWithFormat에서이를 수행하는 것이 바람직하지는 않지만 여러 번 호출 할 필요는 없습니다.
+0

놀이터보기의 배경색을 설정해 보았습니다. 보기의 프레임은 예상대로입니다. 포함 된 뷰 요소가 표시되지 않습니다. 코드에서 볼 수 있듯이 해당 뷰의 배경을 설정하려고했습니다. – userx

+0

subview에 직접 설정하는 대신 playGroundView에 제약 조건을 추가 할 수도 있습니다. 'self.playGroundView.addConstraint (NSLayoutConstraint (item : publishButton, attribute : .height, relatedBy :. equal, toItem : nil, 속성 : .notAnAttribute, 배율 : 1, 상수 : 44)) ' –

+0

하위 뷰를 의심하고 있습니다. '프레임이 0이므로 배경색조차도 표시되지 않습니다. –

관련 문제