Ich habe eine UITableView mit benutzerdefinierten Zellen. Die Struktur jeder Zelle ist wie folgt: Ich habe contentView, in dieser contentView habe ich backView (einfache UIView mit weißem Hintergrund und eckigen Radius 16.0), in dieser BackView habe ich eine imageView mit einem Bild.ClipsToBounds funktioniert nicht in Zellen
Was ich will, ist dieses ImageView (in seiner Eltern UIView - BackView - Grenzen) in die Enge getrieben haben. Und so funktioniert es nicht.
Der Code ist recht einfach (von ImageCell.swift):
self.backView = UIView()
self.backView.backgroundColor = UIColor.white
self.backView.translatesAutoresizingMaskIntoConstraints = false
self.backView.layer.cornerRadius = 16.0
self.contentView.addSubview(backView)
self.picture = UIImageView()
self.picture.translatesAutoresizingMaskIntoConstraints = false
self.picture.contentMode = UIViewContentMode.scaleAspectFill
self.picture.backgroundColor = UIColor.gray
self.picture.clipsToBounds = true
self.backView.addSubview(picture)
let constraintPicTop = NSLayoutConstraint(item: picture, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .topMargin, multiplier: 1.0, constant: -6)
let constraintPicLeft = NSLayoutConstraint(item: picture, attribute: .left, relatedBy: .equal, toItem: backView, attribute: .leftMargin, multiplier: 1.0, constant: -8)
let constraintPicRight = NSLayoutConstraint(item: picture, attribute: .right, relatedBy: .equal, toItem: backView, attribute: .rightMargin, multiplier: 1.0, constant: 8)
constraintBottomPic = NSLayoutConstraint(item: picture, attribute: .bottom, relatedBy: .lessThanOrEqual, toItem: contentView, attribute: .topMargin, multiplier: 1.0, constant: 150)
Ich weiß nicht, die Größe des Bildes im voraus, so constraintBottomPic Wert wird in cellForRowAt Funktion aktualisieren.
Und es funktioniert, außer dieses Bild ist nicht in die Ecke (und ich glaube, es sollte sein).
(Es ist mir leider nicht möglich, cornerRadius für UIImageView zu setzen).
Aktualisierung: Die Lösung gefunden. Es scheint, dass ich 'clipsToBounds' in allen Elternansichten direkt auf true setzen musste (contentView und backView, in meinem Fall).
clipsToBounds sollte für die Elternansicht der Bildansicht gesetzt werden, fügen Sie dieses self.backView.clipsToBounds = true hinzu. ich hoffe es hilft. – luckyShubhra