2013-10-17 170 views
-1

這是我的代碼,應該工作,但沒有。爲什麼我不能創建邊框

self.square =[[UIView alloc]init]; 
self.square.center = CGPointMake(location.x, location.y); 
//_square.backgroundColor = [UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0 alpha:1]; 
[_square.layer setBorderColor: (__bridge CGColorRef)[UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0 alpha:1]]; 
_square.alpha=1.0; 
[_square.layer setBorderWidth:2.0]; 
[previewView addSubview:_square]; 

事情是,如果我給視圖的背景顏色,然後視圖是可見的,但如果我不,視圖不是。我想要做的是獲得一個方形輪廓。所以我想如果我看到一個沒有背景色和邊框的視圖,它就會起作用。

+0

如果你downvote問題,有正派說明原因 – nupac

回答

1

默認的UIView是透明的。您可以通過爲視圖的背景和邊框設置不同的顏色來實現自己想要的效果。然後根據需要進行調整。

你可以這樣做:

_square.backgroundColor = [UIColor whiteColor]; 
[_square.layer setBorderColor:[UIColor redColor].CGColor]; 
3

不強制轉換下面的代碼的UIColor到CGColor 試

[_square.layer setBorderColor: [UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0 alpha:1].CGColor]; 
+0

謝謝你,我加了類型轉換,因爲我得到了一個錯誤。不知道'.CGColor'是一件東西 – nupac

+2

@nupac你所需要做的就是查看'UIColor'的文檔以查看'CGColor'屬性。 – rmaddy

+0

冷靜下來的人,文檔是巨大的,有時你會錯過一些屬性。我問,你回答說,在那裏結束 – nupac