2017-07-08 91 views
1

我有一個帶有自定義單元格的UITableView。每個單元格的結構都是這樣的:我有contentView,在這個contentView中我有backView(帶有白色背景和角度半徑16.0的簡單UIView),在這個backView中,我有一個imageView和一些圖片。ClipsToBounds在單元格中不起作用

我想要的是讓這個圖像視圖(在他的父母的UIView - backView邊框內)。而且它不以這種方式工作。

的代碼是(從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) 

我不知道圖像的大小提前,所以constraintBottomPic值在cellForRowAt功能更新。

它的工作,除了這個形象沒有角逐(我相信它應該是)。

(不幸的是,我無法爲UIImageView設置cornerRadius)。

更新:找到解決方案。看來我必須直接在所有父視圖中設置「clipsToBounds」爲true(contentView和backView,在我的情況下)。

+0

clipsToBounds應該爲圖像視圖的父視圖添加self.backView.clipsToBounds = true。希望能幫助到你。 – luckyShubhra

回答

1

應設置更高級別的容器視圖的clipsToBounds財產(如您的細胞的contentView

+1

是的,我發現我自己,但謝謝你的迴應。 – lithium

1

應用imageView.layer.maskToBounds = YES;

應用此查看或您要設定圓角半徑ImageView的。

正如你所提到的圓角半徑,以您的觀點,你需要設置你的觀點

相關問題