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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19008168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:41:41  来源:igfitidea点击:

Create circle button in iOS7 without images

iosuibuttonios7

提问by henning77

I would like to recreate the circular buttons found in the Clock app in iOS7. The buttons are basically circles with different appearance depending on the button states (green border, red border, grey fill).

我想重新创建在 iOS7 的时钟应用程序中找到的圆形按钮。根据按钮状态(绿色边框、红色边框、灰色填充),按钮基本上是具有不同外观的圆形。

I could of course achieve this using a simple UIButton with images for the different states.

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

However I am looking for a solution which draws the circle programmatically, so I can easily change radius, stroke width, etc.

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

As far as I can see UIButton only allows me to define an UIImage for each state, so I cannot modify the layers per state directly (e.g. provide a layer with cornerRadius). Is there another way?

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

回答by caglar

Creating a custom button may be helpful.

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

in the .h file;

在 .h 文件中;

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

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

@end

in the .m file;

在 .m 文件中;

    #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

And in the view controller, call [self.myCircleButton drawCircleButton:[UIColor myColor]]

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

回答by Peter Sarnowski

There are lot of ways you could accomplish this, for example:

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

  • Use CAShapedLayer

  • Subclass UIViewand use the drawRect:method to draw a circle

  • Just have a square UIViewand use the layer.cornerRadiusproperty.

  • 使用CAShapedLayer

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

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

Depending on your needs, something as simple as creating normal UIButton and calling

根据您的需要,像创建普通 UIButton 和调用一样简单

myButton.layer.cornerRadius = myButton.bounds.size.width / 2.0;

could work (you'll need to include the QuartzFramework)

可以工作(你需要包含Quartz框架)

回答by Kevin ABRIOUX

Solution with a swift Extension :

快速扩展的解决方案:

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

Just call

打电话就行

myView.asCircle()

Can work with any type of view, not only a button

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

回答by Mike Pollard

The pattern I've used to achieve this kind of thing is:

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

Subclass UIButtonand implement drawRectto draw the button as you want, customising colours based on the selectedand highlightedproperties of the button.

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

Then override setSelectedand setHighlightedto force redraws like so:

然后像这样覆盖setSelectedsetHighlighted强制重绘:

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

回答by wmsschlck

Add this code

添加此代码

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

where imagePreview is UIview/UIimageview/UIButton

其中 imagePreview 是 UIview/UIimageview/UIButton

don't forget to add

不要忘记添加

#import <QuartzCore/QuartzCore.h>

回答by Abo3atef

You can use this control, it's subclass from UIButton. https://github.com/alvaromb/AMBCircularButton

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

回答by Aya Aboud

I think it work good.

我认为它工作得很好。

override func viewDidLoad() {
    super.viewDidLoad()

    var button = UIButton.buttonWithType(.Custom) as UIButton
    button.frame = CGRectMake(160, 100, 50, 50)
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)
    button.addTarget(self, action: "thumbsUpButtonPressed", forControlEvents: .TouchUpInside)
    view.addSubview(button)
}

func thumbsUpButtonPressed() {
    println("thumbs up button pressed")
}

回答by Kernelzero

  1. In storyboard, Make square UIButton (eg. 100 * 100 )

  2. Link outlet (eg. @property (Strong, nonatomic) UIButton IBOutlet * myButton; )

  3. In your code write this: self.myButton.layer.cornerRadius = 50;// 50 is half of square's length of one side.

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

  2. 链接出口(例如@property (Strong, nonatomic) UIButton IBOutlet * myButton; )

  3. 在你的代码中这样写: self.myButton.layer.cornerRadius = 50; // 50 是正方形一侧长度的一半。