Ilie
Ilie

Reputation: 23

Constraints for UIcollectionView

In ViewController I insert CollectionView using this code:

HomeMenuView = UICollectionView(frame: self.view.bounds, collectionViewLayout: UICollectionViewFlowLayout())

I want to use constraints with visual format to frame the CollectionView between two objects( Top Image, bottom: button).

PS: Without using storyboard, code only.

Upvotes: 1

Views: 408

Answers (2)

Bliss
Bliss

Reputation: 575

take look at this awesome pod to build dynamic and beautiful user interfaces like a boss, with Swift - NEON

there, you can just add

yourView.alignBetweenVertical(align: .AboveCentered, primaryView: yourTopView, secondaryView: yourBottomView, padding: padding, width: size)

or any other code snippets from the link I provided. All constraints with one line of a code.

P.S. I wouldn't name your object instances with Capital Letter, it is not a class name, so instead of HomeMenuView - call it homeMenuView, look at code convention - RayWanderlichCodeConvention

Upvotes: 1

Reinier Melian
Reinier Melian

Reputation: 20804

I assume that your ImageView are added and properly setup with the needed constraints, also your button,

self.addSubview(HomeMenuView);
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[HomeMenuView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["HomeMenuView":HomeMenuView,"imageView":self.yourImageView,"button":self.yourButton]));
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[imageView]-0-[HomeMenuView]-0-[button]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["HomeMenuView":HomeMenuView,"imageView":self.yourImageView,"button":self.yourButton]));

I also suggest you check this https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html

I hope this helps

Upvotes: 0

Related Questions