Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 round framed button

the iOS App Store has a blue round framed button for buying/downloading apps.

In my App you can download additional content and I want to have a similar button, just because it looks familiar to the user.

If you don't know, what I mean: these buttons, like "$3.99"

enter image description here

How is this possible?

like image 653
h345k34cr Avatar asked Oct 02 '13 17:10

h345k34cr


4 Answers

You can manipulate the CALayer of your button to do this pretty easily.

// 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)

you can tweak each of those to get the color and border effect you want.

You will also have to add an import in any file you want to use CALayers

#import <QuartzCore/QuartzCore.h>
like image 169
Dima Avatar answered Oct 10 '22 22:10

Dima


If you are a big fan of using storyboards for your UI design with iOS.. you can set the corner radius (and whichever other parameters as mentioned in dima's answer -- although unfortunately not the color, since it's a CGColor and Apple presently does not have that option in the popup) in the identity inspector->user defined runtime attributes in storyboard as shown here:

enter image description here

bonus: you use runtime attributes for UIButton placeholder text color (see here) and to change fonts of UILabel, UITextField and UIButton as well (see here)

like image 25
abbood Avatar answered Oct 10 '22 20:10

abbood


For standard iOS control elements like UIButton, UILabel, you should use the UIView tintColor property:

buyButton.layer.borderColor = buyButton.tintColor.CGColor;
like image 17
Gideon King Avatar answered Oct 10 '22 22:10

Gideon King


For simple border like you described, use the answer from Dima using CALAyer.

If you want more, e.g a rounded rectangle with gradient then use this approach as base:

Create a custom View which draws a rounded rectangle and place it over the button. Use the search function here to search for draw rounded rectangle. The drawing works by drawing 4 arcs with defined radius (corners) and 4 straight lines.


FTR, here's how you make a UIView with the correct iOS7 rounded corners, per Alex and Brian.

I'm pretty sure that CGPathCreateWithRoundedRect does not give you the "new" rounded corners. You have to use bezierPathWithRoundedRect for the "new" corners.

#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

enter image description here

enter image description here

Hope it helps someone

like image 8
AlexWien Avatar answered Oct 10 '22 21:10

AlexWien