Swift iOS-标记集合视图

Swift iOS-标记集合视图,ios,swift,tags,Ios,Swift,Tags,我正在编写我的第一个iOS应用程序,我想回答一下,最著名的解决方案是什么?这是一个简单的标签收集。我已经在网上查过了,但什么也没找到。我认为最好的方法是制作我自己的按钮结构,也许 以下是我想要实现的目标: 只需在superView中动态添加按钮,并根据需要更改背景。我使用集合视图解决了这个问题 class FilterController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSour

我正在编写我的第一个iOS应用程序,我想回答一下,最著名的解决方案是什么?这是一个简单的标签收集。我已经在网上查过了,但什么也没找到。我认为最好的方法是制作我自己的按钮结构,也许

以下是我想要实现的目标:


只需在superView中动态添加按钮,并根据需要更改背景。

我使用集合视图解决了这个问题

class FilterController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    @IBOutlet var collectionView: UICollectionView?


    override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view, typically from a nib.
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 150, left: 10, bottom: 150, right: 10)
    // layout.itemSize = CGSize(width: 90, height: 45)
    layout.itemSize = CGSizeFromString("Aloha")

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView!.dataSource = self
    collectionView!.delegate = self
    collectionView!.registerClass(TagCell.self, forCellWithReuseIdentifier: "TagCell")
    collectionView!.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(collectionView!)
}

有时你需要自己动手:

import UIKit
import PlaygroundSupport

class TagsView: UIView {

    // MARK: - Properties

    var offset: CGFloat = 5

    // MARK: - Public functions

    func create(cloud tags: [UIButton]) {
        var x = offset
        var y = offset
        for (index, tag) in tags.enumerated() {
            tag.frame = CGRect(x: x, y: y, width: tag.frame.width, height: tag.frame.height)
            x += tag.frame.width + offset

            let nextTag = index <= tags.count - 2 ? tags[index + 1] : tags[index]
            let nextTagWidth = nextTag.frame.width + offset

            if x + nextTagWidth > frame.width {
                x = offset
                y += tag.frame.height + offset
            }

            addSubview(tag)
        }
    }
}

private func button(with title: String) -> UIButton {
    let font = UIFont.preferredFont(forTextStyle: .headline)
    let attributes: [NSAttributedString.Key: Any] = [.font: font]
    let size = title.size(withAttributes: attributes)

    let button = UIButton(type: .custom)
    button.setTitle(title, for: .normal)
    button.titleLabel?.font = font
    button.setTitleColor(.darkGray, for: .normal)
    button.layer.borderWidth = 1.0
    button.layer.cornerRadius = size.height / 2
    button.layer.borderColor = UIColor.darkGray.cgColor
    button.frame = CGRect(x: 0.0, y: 0.0, width: size.width + 10.0, height: size.height + 10.0)
    button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 5.0, bottom: 0.0, right: 5.0)
    return button
}

let titles = ["Freedom", "God", "Happiness", "Imagination", "Intelligence", "Other"]
let tags = titles.map { button(with: $0) }

let frame = CGRect(x: 0, y: 0, width: 260, height: 200)
let tagsView = TagsView(frame: frame)
tagsView.backgroundColor = .white
tagsView.create(cloud: tags)

PlaygroundPage.current.liveView = tagsView
PlaygroundPage.current.needsIndefiniteExecution = true
导入UIKit
导入PlaygroundSupport
类标记视图:UIView{
//标记:-属性
变量偏移量:CGFloat=5
//马克:-公共职能
func创建(云标记:[UIButton]){
var x=偏移量
变量y=偏移量
对于tags.enumerated()中的(索引,标记){
tag.frame=CGRect(x:x,y:y,宽度:tag.frame.width,高度:tag.frame.height)
x+=标记.frame.width+偏移量
设nextTag=index frame.width{
x=偏移量
y+=标记.框架.高度+偏移
}
添加子视图(标记)
}
}
}
专用func按钮(标题:字符串)->UIButton{
让font=UIFont.preferredFont(forTextStyle:.headline)
let属性:[NSAttributedString.Key:Any]=[.font:font]
let size=title.size(withAttributes:attributes)
let button=ui按钮(类型:。自定义)
button.setTitle(标题,用于:。正常)
按钮。标题标签?.font=font
button.setTitleColor(.darkGray,用于:。正常)
button.layer.borderWidth=1.0
button.layer.cornerRadius=尺寸.高度/2
button.layer.borderColor=UIColor.darkGray.cgColor
button.frame=CGRect(x:0.0,y:0.0,宽度:size.width+10.0,高度:size.height+10.0)
button.titleEdgeInsets=UIEdgeInsets(顶部:0.0,左侧:5.0,底部:0.0,右侧:5.0)
返回按钮
}
让标题=[“自由”、“上帝”、“幸福”、“想象”、“智慧”、“其他”]
let tags=titles.map{按钮(带:$0)}
设frame=CGRect(x:0,y:0,宽度:260,高度:200)
设tagsView=tagsView(帧:帧)
tagsView.backgroundColor=.white
tagsView.create(云:标记)
PlaygroundPage.current.liveView=tagsView
PlaygroundPage.current.NeedsDefiniteExecution=true

我只使用collectionview实现了一个tagview


是github链接。

您可以使用一组按钮,也可以使用集合视图,但不要在internet上到处寻找解决方案;阅读文档并学习基础知识,这样你就可以自己做事情了。是的,但我认为更有用的是当我学习到最佳实践,并在这里用代码给出答案。谢谢你,伙计!它对我有用,现在我在另一个集合视图帖子中对它进行了样式化。这里:使用
CGSizeFromString(“Aloha”)
的方式不正确。请参阅文档,它不起作用。标记列表项通常是左对齐的,在这个问题中,它们是居中对齐的。但是在您的解决方案中,它们填充屏幕宽度(项目之间的间距增加到每行的全填充),您能够获得集合视图的高度吗?