[英]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.