看看下面的圖片:如何從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