ios – 如何从UIButton控件中删除尖锐的矩形边?

前端之家收集整理的这篇文章主要介绍了ios – 如何从UIButton控件中删除尖锐的矩形边?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
看一下下图:

正如您所看到的那样,边缘是尖锐的矩形,它们是从圆角按钮出来的.我怎么能不显示边缘,只显示圆形按钮?

更新(解决方案):

我使用以下代码来实现圆角:

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>

希望这可以帮助

蒂姆

原文链接:https://www.f2er.com/iOS/335282.html

猜你在找的iOS相关文章