[英]How to remove sharp rectangle edges from UIButton control?
看一下下圖:
正如您所看到的那樣,邊緣是尖銳的矩形,它們是從圓角按鈕出來的。 我怎么能不顯示邊緣而只顯示圓形按鈕?
更新(解決方案):
我使用以下代碼來實現圓角:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.stopButton.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(12.0, 12.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.stopButton.bounds;
maskLayer.path = maskPath.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
self.stopButton.layer.mask = maskLayer;
新問題:
這段代碼給了我startButtonRound.layer.cornerRadius行的錯誤。
UIButton *roundStartButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
roundStartButton.frame = CGRectMake(10, 10, 100, 20);
[roundStartButton setBackgroundImage:[UIImage imageNamed:@"green_gradient.jpg"] forState:UIControlStateNormal];
roundStartButton.layer.cornerRadius = 12.0;
UIBarButtonItem *startButton = [[UIBarButtonItem alloc] initWithCustomView:roundStartButton];
你可以單獨用CALayer做到這一點:
CALayer *myLayer = [CALayer layer];
[myLayer setMasksToBounds:YES];
[myLayer setCornerRadius:5.0f];
[myLayer setBorderWidth:1.0f];
[myLayer setBorderColor: [[UIColor blackColor] CGColor]];
[myLayer setBackgroundColor: [[UIColor whiteColor] CGColor]];
您還需要包含QuartzCore框架並在您自己的框架中包含框架標頭。
#include <QuartzCore/QuartzCore.h>
希望這可以幫助
蒂姆
我想所有這些都可以通過以下方式實現:
self.stopButton.layer.cornerRadius = 12.0;
但你試過嗎?
self.stopButton.opaque = NO;
self.stopButton.backgroundColor = [UIColor clearColor];
?
形成您將UIButton背景顏色設置為“白色”的圖像外觀。 使它'清除顏色'。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.