看看下面的图片:如何从UIButton控件中删除尖锐的矩形边缘?
正如你所看到的边缘锐利的矩形,他们走出圆角按钮。我怎样才能不显示边缘,只显示圆形按钮?
UPDATE(解决方案):
我用下面的代码来实现圆角:
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];
我尝试过[button layer] setCornerRadius:5.0f];但它对按钮没有任何影响。 – azamsharp
您可能需要将MasksToBounds设置为yes才能让图层将其内容剪裁为圆角。 –
是否必须将UIButton声明为cornerRadius的IBOutlet才能使用?查看原来的帖子更新问题。 – azamsharp