Swift autolayout : constraints in sub-view not working

userx

I am trying to set up a view with two collection views and a view on the center , which in turn has elements inside it.

I have been trying to set constraints using visual format like this :

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))
    }

I don't see the elements inside the sub-view (playGroundView) being rendered. Can someone please suggest what am I doing wrong here ?

Hong Wei

I would try the following to debug:

  1. Temporarily set a solid background color to playGroundView instead of UIColor.clear and run it to make playGroundView is having a correct size and position.
  2. Temporarily set borders to the missing subviews and run it to see if the views are even within the playGroundView bounds. Alternatively, you can also run the code, go to Xcode > Debug > View Debugging > Capture View Hierarchy
  3. Set translatesAutoresizingMaskIntoConstraints = false for publishButton, searchBar, etc before adding to playGroundView. While it is not wrong to perform that in your helper function addConstraintsWithFormat, there is no need to make that call multiple times.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift: Programmatically set autolayout constraints for subviews don't resize view

From Dev

Autolayout constraints not working iPhone 6 and 6 Plus

From Dev

Swift: Change autolayout constraints when keyboard is shown

From Dev

Using Scroll View with Autolayout Swift

From Dev

Using Scroll View with Autolayout Swift

From Dev

Autolayout: Fallback constraints for neighbouring views when removing a view

From Dev

Adding View, Removing it, and Adding it Again Breaks AutoLayout Constraints

From Dev

Updating Constraints/Frames After a View is Programmatically Removed from Superview (Autolayout)

From Dev

Custom autolayout constraints broken when adding a controller's view programmatically

From Dev

When the autolayout constraints set frames during view controller life cycle?

From Dev

iOS add view as subview programatically autolayout conflicting constraints

From Dev

autolayout constraints not working correctly when adding them programmatically

From Dev

Swift : update of view constraints not visible

From Dev

Swift Type Constraints not working as expected

From Dev

Swift: I'm not able to make autolayout constraints work for my UIScrollView

From Dev

Swift: Autolayout constraints issue. Portrait vs Landscape

From Dev

AutoLayout constraints and UIPopoverPresentationController

From Dev

UITableViewCellAccessoryCheckmark and AutoLayout constraints

From Dev

Xcode Autolayout Constraints Confusion

From Dev

AutoLayout, Constraints and Animation

From Dev

Autolayout constraints conflict

From Dev

Something like autolayout or constraints

From Dev

Debugging programmatic autolayout constraints

From Dev

AutoLayout Constraints or StackView

From Dev

AutoLayout, Constraints and Animation

From Dev

Something like autolayout or constraints

From Dev

Autolayout & Alignment Constraints

From Dev

iOS Constraints - Autolayout

From Dev

View shows sub view outside of it in swift

Related Related

  1. 1

    Swift: Programmatically set autolayout constraints for subviews don't resize view

  2. 2

    Autolayout constraints not working iPhone 6 and 6 Plus

  3. 3

    Swift: Change autolayout constraints when keyboard is shown

  4. 4

    Using Scroll View with Autolayout Swift

  5. 5

    Using Scroll View with Autolayout Swift

  6. 6

    Autolayout: Fallback constraints for neighbouring views when removing a view

  7. 7

    Adding View, Removing it, and Adding it Again Breaks AutoLayout Constraints

  8. 8

    Updating Constraints/Frames After a View is Programmatically Removed from Superview (Autolayout)

  9. 9

    Custom autolayout constraints broken when adding a controller's view programmatically

  10. 10

    When the autolayout constraints set frames during view controller life cycle?

  11. 11

    iOS add view as subview programatically autolayout conflicting constraints

  12. 12

    autolayout constraints not working correctly when adding them programmatically

  13. 13

    Swift : update of view constraints not visible

  14. 14

    Swift Type Constraints not working as expected

  15. 15

    Swift: I'm not able to make autolayout constraints work for my UIScrollView

  16. 16

    Swift: Autolayout constraints issue. Portrait vs Landscape

  17. 17

    AutoLayout constraints and UIPopoverPresentationController

  18. 18

    UITableViewCellAccessoryCheckmark and AutoLayout constraints

  19. 19

    Xcode Autolayout Constraints Confusion

  20. 20

    AutoLayout, Constraints and Animation

  21. 21

    Autolayout constraints conflict

  22. 22

    Something like autolayout or constraints

  23. 23

    Debugging programmatic autolayout constraints

  24. 24

    AutoLayout Constraints or StackView

  25. 25

    AutoLayout, Constraints and Animation

  26. 26

    Something like autolayout or constraints

  27. 27

    Autolayout & Alignment Constraints

  28. 28

    iOS Constraints - Autolayout

  29. 29

    View shows sub view outside of it in swift

HotTag

Archive