2015-08-25 26 views
9

我有需要的視圖和輪次角碼 - 把它變成一個圓圈:圓角的觀點不是在iOS的工作9

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)]; 
imageView.layer.cornerRadius = imageView.frame.size.width/2; 
imageView.layer.masksToBounds = YES; 

這適用於iOS 7和iOS的8大 - 但不在iOS 9上工作。 我該如何解決它?

+0

如果這是真的不工作,我的應用程序會崩潰:/ –

+0

這不是崩潰 - 但它保持矩形 – YogevSitton

+0

我只是檢查,但它確實工作。你可以改變dot.frame.size.width/2來說4嗎? – tafh

回答

9

原來添加背景顏色的ImageView的修復問題...

+0

如果這是答案,你應該接受它。 –

+0

我同意 - 但我必須等到明天... – YogevSitton

+0

一旦clearColor不起作用,它必須是合適的顏色。 – Linox83

0

如果這不是工作了,做一個組成部分。 UIView的子類。請在此子類上更改:

subclass.layer.cornerRadius = subclass.frame.size.width/2; 
subclass.layer.masksToBounds = YES; 

並將imageView添加到此視圖。這將解決問題。

@implementation CircleImageView{ 

    UIImageView* imageView; 
    CGFloat width; 

} 

- (instancetype) initWithTopLeft:(CGPoint)topLeft width:(CGFloat)wdth{ 

    self = [super initWithFrame:CGRectMake(topLeft.x, topLeft.y, wdth, wdth)]; 
    if (self) { 
     width = wdth; 

     [self createContent]; 

     self.layer.cornerRadius = width/2.; 
     self.clipsToBounds = YES; 
    } 
    return self; 
} 

- (void) createContent{ 
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)]; 
    [self addSubview:imageView]; 

} 
+1

如果您不知道問題的原因,您如何知道這將解決問題? –

+0

我正在使用我的圈子imageViews,因爲有一次錯誤,當我無法設置imageView的cornerRadius。 – incmiko

+0

你的確設置了圖層的圓角半徑,就像他一樣。爲什麼應該在UIImage的子類上工作,而不是在iOS 9中的UIImage上工作? –

0

嘗試鏈接框架和庫添加Quartzcore.framework

然後導入相同的在viewController.m文件

0

我jsut的子類UIImageView,像這樣,我使用PureLayout控制imageView,如下所示:

[imageViewIcon autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.contentView withOffset:20]; 
[imageViewIcon autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.contentView withOffset:10]; 
[imageViewIcon autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.contentView withOffset:-10]; 
[imageViewIcon autoMatchDimension:ALDimensionHeight toDimension:ALDimensionWidth ofView:imageViewIcon]; 




-(instancetype)init 
{ 
if (self = [super init]) { 

} 

return self; 
} 
-(void)layoutSubviews 
{ 
[super layoutSubviews]; 
[self layoutIfNeeded]; 

self.layer.masksToBounds = YES; 
self.layer.cornerRadius = self.width*0.5; 



} 
0

多虧了我已經搜查過的文檔,發現@YogevSitton回答:

這似乎是東西apple docs

默認情況下,圓角半徑不適用圖層的 圖層的內容屬性;它僅適用於圖層的背景顏色和邊框。

所以,要設置cornerRadius到UIImageView它需要有backgroundColor。