在iOS7中创建没有图像的圆形按钮

时间:2013-09-25 14:48:41

标签: ios uibutton ios7

我想重新创建iOS7中Clock应用程序中的圆形按钮。按钮基本上是具有不同外观的圆形,具体取决于按钮状态(绿色边框,红色边框,灰色填充)。

我当然可以使用一个带有不同状态图像的简单UIButton来实现这一点。

但是我正在寻找一种以编程方式绘制圆的解决方案,因此我可以轻松地更改半径,笔触宽度等。

据我所知,UIButton只允许我为每个状态定义一个UIImage,所以我无法直接修改每个状态的层(例如,提供一个带有cornerRadius的图层)。还有另一种方式吗?

7 个答案:

答案 0 :(得分:30)

创建自定义按钮可能会有所帮助。

<。>文件中的

;

#import <UIKit/UIKit.h>
@interface CircleLineButton : UIButton

- (void)drawCircleButton:(UIColor *)color;

@end
<。>文件中的

;

    #import "CircleLineButton.h"

    @interface CircleLineButton ()

    @property (nonatomic, strong) CAShapeLayer *circleLayer;
    @property (nonatomic, strong) UIColor *color;
    @end

    @implementation CircleLineButton



    - (void)drawCircleButton:(UIColor *)color
    {
        self.color = color;

        [self setTitleColor:color forState:UIControlStateNormal];

        self.circleLayer = [CAShapeLayer layer];

        [self.circleLayer setBounds:CGRectMake(0.0f, 0.0f, [self bounds].size.width,
                                      [self bounds].size.height)];
        [self.circleLayer setPosition:CGPointMake(CGRectGetMidX([self bounds]),CGRectGetMidY([self bounds]))];

        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];

        [self.circleLayer setPath:[path CGPath]];

        [self.circleLayer setStrokeColor:[color CGColor]];

        [self.circleLayer setLineWidth:2.0f];
        [self.circleLayer setFillColor:[[UIColor clearColor] CGColor]];

        [[self layer] addSublayer:self.circleLayer];
    }

    - (void)setHighlighted:(BOOL)highlighted
    {
        if (highlighted)
        {
            self.titleLabel.textColor = [UIColor whiteColor];
            [self.circleLayer setFillColor:self.color.CGColor];
        }
        else
        {
            [self.circleLayer setFillColor:[UIColor clearColor].CGColor];
            self.titleLabel.textColor = self.color;
        }
    }

@end

在视图控制器中,调用[self.myCircleButton drawCircleButton:[UIColor myColor]]

答案 1 :(得分:11)

有很多方法可以实现这一目标,例如:

  • 使用 CAShapedLayer

  • 子类 UIView 并使用 drawRect:方法绘制圆圈

  • 只需要一个方格 UIView 并使用 layer.cornerRadius 属性。

根据您的需要,可以像创建普通UIButton和调用

一样简单
myButton.layer.cornerRadius = myButton.bounds.size.width / 2.0;

可行(您需要包含 Quartz 框架)

答案 2 :(得分:4)

我用来实现这种事情的模式是:

子类UIButton并实施drawRect以根据需要绘制按钮,根据按钮的selectedhighlighted属性自定义颜色。

然后覆盖setSelectedsetHighlighted以强制重绘:

-(void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    [self setNeedsDisplay];
}

答案 3 :(得分:4)

添加此代码

imagePreview.layer.cornerRadius = (imagePreview.bounds.size.height/2);

其中imagePreview是UIview / UIimageview / UIButton

不要忘记添加

#import <QuartzCore/QuartzCore.h>

答案 4 :(得分:4)

快速扩展的解决方案:

extension UIView{
    func asCircle(){
        self.layer.cornerRadius = self.frame.size.width / 2;
        self.layer.masksToBounds = true
    }
}

致电

myView.asCircle()

可以使用任何类型的视图,而不仅仅是按钮

答案 5 :(得分:0)

您可以使用此控件,它是UIButton的子类。 https://github.com/alvaromb/AMBCircularButton

答案 6 :(得分:-1)

  1. 在故事板中,制作方形UIButton(例如100 * 100)

  2. 链接插座(例如@property(强,非原子)UIButton IBOutlet * myButton;)

  3. 在您的代码中写下: self.myButton.layer.cornerRadius = 50; // 50是一边的正方形长度的一半。