简体   繁体   English

UICollectionViewCell sizeForRowAtIndexPath和两个视图并排

[英]UICollectionViewCell sizeForRowAtIndexPath and Two Views side by side

I am trying to accomplish what the Imgur app has done in one of their UICollectionViewCells in their about view. 我正在尝试完成Imgur应用程序在其About视图中的其中一个UICollectionViewCells中所做的事情。 See below: 见下文:

在此处输入图片说明

I want 1 UICollectionViewCell but instead of 3 sections of views inside the cell, I just want two (comments and posts). 我想要1个UICollectionViewCell,但我只想要两个(注释和帖子),而不是单元格内部的3个视图部分。

For my UICollectionView I am resizing the cell at runtime to make it fit almost the whole screen minus a bit for margin's sake because I want to make a card view like Imgur. 对于我的UICollectionView,我正在运行时调整单元格的大小,以使其几乎适合整个屏幕,但为了保证裕量,请稍稍减少一点,因为我想制作像Imgur这样的卡片视图。

I followed this stackoverflow post to get 2 views side by side: iOS Autolayout: two buttons of equal width, side by side 我关注了这个stackoverflow帖子,并排获得了2个视图: iOS Autolayout:两个宽度相等的按钮并排

and I am able to get 2 views side by side of equal width. 而且我可以并排获得两个等宽的视图。 Here's what they look like in the storyboard: 这是情节提要中的外观:

在此处输入图片说明 but when I run the app, my views get stretched because of the autoresizing. 但是当我运行该应用程序时,由于自动调整大小,我的视图变得捉襟见肘。 See below: 见下文:

在此处输入图片说明

How can I get the views to both be of equal width and have equal spacing between, before and after the views? 如何使视图的宽度相等,并且在视图之间,视图之间和视图之间具有相等的间距?

You can use UIStackView . 您可以使用UIStackView It provides an easy way to lay out a series of views horizontally or vertically. 它提供了一种简单的方法来水平或垂直地布置一系列视图。

Select the two views and click on the new Stack button in the Auto Layout toolbar at the bottom right of the storyboard canvas: 选择两个视图,然后在情节提要画布右下角的“自动布局”工具栏中单击新的“堆栈”按钮:

在此处输入图片说明

Give constraints to your stack view from top, left, right and bottom. 从顶部,左侧,右侧和底部为堆栈视图设置约束。 Then make your Attributes Inspector of the stack view like this: 然后使您的堆栈视图的属性检查器如下所示:

在此处输入图片说明

NB. 注意 If you want to learn more about Stack View, follow this fantastic tutorial in Ray Wenderlich: https://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views 如果您想了解有关堆栈视图的更多信息,请遵循Ray Wenderlich中的该出色教程: https ://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views

Believe me most of the complexities associated with Autolayout will become trivial once you learned Stack View 相信我,一旦您学会了堆栈视图,与自动布局相关的大多数复杂性将变得微不足道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM