chetbaker
chetbaker

Reputation: 29

How do I send subviews to front programmatically? (have tried bringSubview to front)

In my UICollectionViewCell I have an image and a label. The picture takes up the whole cell (which I want) - however, the label is placed behind the image, so it's not visible. I have tried bringSubview(toFront: titleLabel), but nothing happens... I got no clue what to do really, have done a lot of searching.

This is the code for the cell, I don't use Storyboard as you can see (sorry for messy constraints, was testing different solutions to find out if this was the problem)

import UIKit

class BaseCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
        setupBasket()

    }

    func setupViews() {

    }

    func setupBasket(){

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class VideoCell: BaseCell {

    var selectedItemID : String!
    static let sharedInstance = VideoCell()
    var video: Video? {
        didSet {
            titleLabel.text = video?.title
            setupThumbnailImage()

        }
    }


    func setupThumbnailImage() {
        if let thumbnailImageUrl = video?.thumbnail_image_name {
            thumbnailImageView.loadImageUsingUrlString(thumbnailImageUrl)
        }
    }

    let thumbnailImageView: CustomImageView = {
        let imageView = CustomImageView()
        imageView.image = UIImage(named: "taylor_swift_blank_space")
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()

    let titleLabel: UILabel = {
        let textView = UILabel()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.text = "Clothes"
        textView.textColor = UIColor.lightGray
        return textView
    }()


    let separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
        return view
    }()

    var titleLabelHeightConstraint: NSLayoutConstraint?

    let addtoBasket = UIButton(type: .contactAdd)


    override func setupViews() {

        addtoBasket.frame = CGRect(x: 50, y: 0, width: 20, height: 60)

        addSubview(addtoBasket)
        addSubview(titleLabel)
        addSubview(thumbnailImageView)
        addSubview(separatorView)
        addSubview(addtoBasket)


        titleLabel.superview!.bringSubview(toFront: titleLabel)


        //horizontal constraints
        addConstraintsWithFormat("H:|-0-[v0]-0-|", views: thumbnailImageView)


        //vertical constraints
        addConstraintsWithFormat("V:|-1-[v0]-1-|", views: thumbnailImageView)
        addConstraintsWithFormat("H:|-0-[v0]-1-|", views: separatorView)


        addtoBasket.translatesAutoresizingMaskIntoConstraints = false
        addtoBasket.heightAnchor.constraint(equalToConstant: 20).isActive = true
        addtoBasket.widthAnchor.constraint(equalToConstant: 20).isActive = true
        addtoBasket.centerXAnchor.constraint(equalTo: addtoBasket.superview!.centerXAnchor, constant: 90).isActive = true
        addtoBasket.centerYAnchor.constraint(equalTo: addtoBasket.superview!.centerYAnchor, constant: -50).isActive = true

        //top constraint
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 8))

        //right constraint
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0))

        //right constraint
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 20))

        //height constraint
        titleLabelHeightConstraint = NSLayoutConstraint(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1, constant: -10)
        addConstraint(titleLabelHeightConstraint!)

    }
}

Upvotes: 1

Views: 154

Answers (2)

chetbaker
chetbaker

Reputation: 29

There was clearly something wrong with the constraints, now working! Thanks

Upvotes: 1

Cyril
Cyril

Reputation: 2818

Try accessing the labels layer and set its zPosition.

Try titleLabel.layer.zPosition = 1

Upvotes: 1

Related Questions