2017-04-06 5 views
1

In meinem UICollectionViewCell habe ich ein Bild und ein Label. Das Bild nimmt die ganze Zelle ein (was ich will) - das Etikett ist jedoch hinter dem Bild platziert, so dass es nicht sichtbar ist. Ich habe versucht bringSubview(toFront: titleLabel), aber nichts passiert ... Ich habe keine Ahnung, was ich wirklich tun muss, habe eine Menge Suche gemacht.Wie sende ich Subviews programmatisch nach vorne? (habe bringSubview nach vorne versucht)

Dies ist der Code für die Zelle, ich verwende nicht Storyboard wie Sie sehen können (sorry für chaotisch Einschränkungen, testete verschiedene Lösungen, um herauszufinden, ob dies das Problem war)

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!) 

    } 
} 
+1

Ihre Einschränkungen sind wahrscheinlich falsch. Fügen Sie jeweils eine Ansicht hinzu und überprüfen Sie, ob jede korrekt ist. –

+0

@LouFranco Ich habe das Bild kleiner gemacht und kann deutlich sehen, dass der Text unter ist, also sollte es nicht die Einschränkungen sein. Die Schaltfläche 'addtoBasket' funktioniert und befindet sich nicht unter dem Bild. Ich habe auch versucht, alle Einschränkungen Praktiken für das Label, aber nichts funktioniert, aber vielleicht gibt es immer noch etwas widersprüchlich .. wirklich seltsam – chetbaker

Antwort

1

Versuchen Sie den Zugriff auf der Etiketten-Layer und legen Sie seine zPosition fest.

Versuchen titleLabel.layer.zPosition = 1

1

Es ist etwas falsch mit den Einschränkungen klar war, jetzt zu arbeiten! Danke