简体   繁体   English

将CollectionView或ViewController添加到AlertView

[英]Add CollectionView or ViewController to AlertView

I have a collection view like this: 我有一个这样的集合视图: 在此处输入图片说明

include textfields and buttons. 包括文本字段和按钮。 I presented this view as a popover but the problem is I want the buttons to be on one line (button is also a cell of collection view) so there are 2 questions: 我将这个视图显示为一个弹出窗口,但是问题是我希望按钮位于一行(按钮也是集合视图的一个单元),所以有两个问题:

  • How to change the buttons size to fit in one line but do not affect the textfield size 如何更改按钮大小以适合一行但不影响文本字段大小
  • Or how to add the collection view to an alert view so I have the action button easier?? 还是如何将集合视图添加到警报视图,以便使操作按钮更容易?

要将按钮设置为一行,您需要更新按钮约束。将两个按钮的宽度都设置为Equal Width并将两个按钮的顶部,底部约束对齐。

Pull both of your button to a stackView and draw only one button's width and height constraint and make the other follow the previous button's width and height. 将两个按钮都拉到stackView上,仅绘制一个按钮的宽度和高度约束,并使另一个按钮遵循上一个按钮的宽度和高度。 stackView will do the rest of the work. stackView将完成其余工作。

Don't forget to add constraint for stackView though. 但是不要忘记为stackView添加约束。 :) :)

在此处输入图片说明

在此处输入图片说明

I just pull width and height constraint to the first button and from stackView view i set fill equally and interdistance 10 我只是将宽度和高度约束拉到第一个按钮,并从stackView视图中设置了均等填充和相距10

在此处输入图片说明

Have you tried implementing a UICollectionViewDelegateFlowLayout ? 您是否尝试过实现UICollectionViewDelegateFlowLayout

If not, try doing that and add the method 如果不是,请尝试这样做并添加方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Implement the code here, check if the indexPaths belong to the buttons,
// And make it return a Size which has a width that is half of the CollectionView width (minus the padding between cells).
}

This lets the CollectionView place them side by side because it'll have enough space for both of them. 这样CollectionView可以并排放置,因为它们有足够的空间容纳它们两个。

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

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