子视图位于边界层下方-Swift-以编程方式

问题描述

我通过这种方式设置UICollectionViewCell的边框:

theView.layer.borderWidth = 5
theView.layer.borderColor = UIColor.white.cgColor

问题是我在该collectionviewcell上添加一个子视图,它像这样在边框下方移动:

enter image description here

我实际上想将子视图带到CollectionViewCell上

将子视图添加到单元格的代码

plusImageView.translatesAutoresizingMaskIntoConstraints = false
    addSubview(plusImageView)
    plusImageView.centerYAnchor.constraint(equalTo: bottomAnchor).isActive = true
    plusImageView.centerXAnchor.constraint(equalTo: trailingAnchor).isActive = true

解决方法

https://developer.apple.com/documentation/uikit/uiview/1622541-bringsubviewtofront

在将子视图添加到父级后,使用parentView.bringSubviewToFront(subview)。