2017-06-09 50 views
1

在我的ViewController插入使用的CollectionView此代码:约束的UIcollectionView

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

我想使用约束用视觉格式到帧的两个对象之间的的CollectionView(顶部图片,下:按键)。

PS:不使用故事板,仅限代码。

回答

0

我假设你的ImageView的加入,并正确设置与必要的约束,也是你的按钮,

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])); 

我也建议你查一下这个https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/VisualFormatLanguage.html

我希望这有助于

+0

我该如何调用:collectionViewLayout:UICollectionViewFlowLayout()? 它会这样工作: HomeMenuView.collectionViewLayout = UIcollectionViewFlowLayout() –

+0

@IlieJeleazcov对不起,我不明白你的问题,你能解释一下你现在需要什么吗? –

+0

For:HomeMenuView = UICollectionView(frame:self.view.bounds,collectionViewLayout:UICollectionViewFlowLayout()),我给框架和collectionViewLayout。 使用你的方法,约束我在做什么与collectionViewLayout,如何声明?它将以这种方式工作:HomeMenuView.collectionViewLayout = UIcollectionViewFlowLayout()?或者我应该做的女巫代码? –

1

起飞看看这个真棒吊舱建立动态和美丽的用户界面像一个老板,与斯威夫特 - NEON

那里,你可以添加

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

或从我提供的链接的任何其他代码片段。一行代码的所有约束。

P.S.我不会用大写字母命名您的对象实例,它不是类名称,所以而不是HomeMenuView - 称为homeMenuView,看看代码约定 - RayWanderlichCodeConvention