iOS 7圆形框架按钮

h34*_*4cr 77 cocoa-touch objective-c uibutton ios

iOS App Store有一个蓝色圆形框架按钮​​,用于购买/下载应用程序.

在我的应用程序中,您可以下载其他内容,我希望有一个类似的按钮,只是因为它看起来很熟悉用户.

如果您不知道,我的意思是:这些按钮,如"3.99美元"

在此输入图像描述

这怎么可能?

Dim*_*ima 199

您可以操作按钮的CALayer来轻松完成此操作.

// assuming you have a UIButton or more generally a UIView called buyButton

buyButton.layer.cornerRadius = 2;
buyButton.layer.borderWidth = 1;
buyButton.layer.borderColor = [UIColor blueColor].CGColor;
// (note - may prefer to use the tintColor of the control)
Run Code Online (Sandbox Code Playgroud)

你可以调整每一个,以获得你想要的颜色和边框效果.

您还必须在要使用CALayers的任何文件中添加导入

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

  • 我创建了一个看起来和按下动画时相同的库.希望这会有所帮助:https://github.com/yhpark/YHRoundBorderedButton (2认同)

abb*_*ood 90

如果你是使用故事板进行iOS设计的忠实粉丝,你可以设置角半径(以及dima的答案中提到的其他参数- 虽然不幸的是不是颜色,因为它是CGColor而Apple现在不弹出窗口中的那个选项identity inspector- > user defined runtime attributes在故事板中,如下所示:

在此输入图像描述

奖金: 您所使用的运行时属性UIButton的占位符文本颜色(参见这里),并改变字体UILabel,UITextFieldUIButton以及(见这里)

  • 啊!请注意,**您无法在界面构建器中设置borderColor的颜色**."颜色"只有一种类型.有没有一种类型的CGColor** - 所以很遗憾你不能这样做!(如果你想要"黑色"作为CGColor,你可以做到!) (3认同)

Gid*_*ing 17

对于像UIButton,的标准iOS控件元素UILabel,您应该使用UIView tintColor属性:

buyButton.layer.borderColor = buyButton.tintColor.CGColor;
Run Code Online (Sandbox Code Playgroud)


Ale*_*ien 8

对于您所描述的简单边框,请使用来自Dima的使用CALAyer的答案.

如果你想要更多,例如带渐变的圆角矩形,那么使用这种方法作为基础:

创建一个自定义视图,绘制一个圆角矩形并将其放在按钮上.使用此处的搜索功能搜索绘制圆角矩形.绘图通过绘制具有定义的半径(角)和4条直线的4个弧来工作.


FTR,这就是你如何使用正确的iOS7圆角制作UIView,每个Alex和Brian.

我很确定CGPathCreateWithRoundedRect 不会给你"新"圆角.你必须使用bezierPathWithRoundedRect作为"新"角.

#import "UIViewWithIOS7RoundedCorners.h"
@implementation UIViewWithIOS7RoundedCorners
-(void)drawRect:(CGRect)rect
    {
    // for a filled shape...

    UIBezierPath* path =
        [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:4];
    [[UIColor blueColor] setFill];
    [path fill];

    // for just a border...

    int thickness=2;
    UIBezierPath* path =
        [UIBezierPath bezierPathWithRoundedRect:
            CGRectInset(self.bounds, thickness/2, thickness/2)
            cornerRadius: 4];
    path.lineWidth = thickness;
    [[UIColor blueColor] setStroke];
    [path stroke];
    }

@end
// ps don't forget to set .backgroundColor=[UIColor clearColor]
// perhaps in initWithCoder/initWithFrame
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述

希望它可以帮助某人

  • 更新,以限制高级绘图的答案 (2认同)
  • 这个解决方案有一些小优势.`+ [UIBezierPath bezierPathWithRoundedRect:cornerRadius:]`使用新的圆角半径样式.此外,您可以生成单个图像以用于所有按钮,从而提高GPU渲染性能. (2认同)