[英]set imageview corner radius
我嘗試了大多數解決方法來設置在單元格中找到的imageview
的角半徑。
但是,它仍然顯示一個框。 我該如何解決?
cell.imageView.image=[UIImage imageNamed:@"def.png"];
cell.imageView.layer.cornerRadius=cell.imageView.frame.size.width/2;
cell.imageView.layer.borderWidth=3.0f;
cell.imageView.layer.borderColor=[UIColor redColor].CGColor;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.shouldRasterize = YES;
嘗試用masksToBounds
替換clipsToBounds
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width/2
cell.imageView.clipsToBounds = YES
確保在cell.imageView.frame.size.width
具有正確的寬度,例如,如果對UIImageView
的寬度使用自動布局,則需要在寬度計算后將代碼移至viewDidLayoutSubviews
視圖可以有選擇地限制其子視圖的繪制,以便不顯示視圖之外的子視圖。 這稱為裁剪,並使用視圖的
clipsToBounds
屬性設置。
嘗試導入QuartzCore
然后使用UIImageView的layer屬性,如下所示:
yourImageView.layer.cornerRadius = 10.0f;
yourImageView.clipsToBounds = YES;
我認為該單元會覆蓋imageView。 嘗試將該代碼粘貼到layoutSubviews
方法中,或將第二個imageView添加到單元格中。
在代碼中,對於layoutSubviews選項,它看起來像這樣:
-(void)layoutSubviews
{
[super layoutSubviews];
cell.imageView.image=[UIImage imageNamed:@"def.png"];
cell.imageView.layer.cornerRadius=cell.imageView.frame.size.width/2;
cell.imageView.layer.borderWidth=3.0f;
cell.imageView.layer.borderColor=[UIColor redColor].CGColor;
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.shouldRasterize = YES;
}
對於第二個imageView這樣的:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"def.png"]];
imageView.frame = CGRectMake(0, 0, 44, 44);
imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.borderWidth = 3.0f;
imageView.layer.borderColor = [UIColor redColor].CGColor;
imageView.layer.masksToBounds = YES;
imageView.layer.shouldRasterize = YES;
[self addSubview:imageView];
您只是缺少clipsToBounds語句。
cell.imageView.clipsToBounds = YES;
加上這個讓我知道結果如何...
如果這不起作用,請嘗試cell.clipsToBounds = YES;
可以肯定的是...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.