Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove sharp rectangle edges from UIButton control?

Tags:

ios

xcode4

Take a look at the following image:

enter image description here

As you can see the edges are sharp rectangle and they are coming out of the rounded corner button. How can I not show the edges and only show the round button?

UPDATE (Solution):

I used the following code to achieve the round corners:

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;

NEW PROBLEM:

This code gives me the error on the startButtonRound.layer.cornerRadius line.

  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];
like image 895
azamsharp Avatar asked Jul 29 '11 17:07

azamsharp


2 Answers

You could do this with CALayer alone:

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]];

You'll also want to include the QuartzCore framework and include the frameworks header in your own.

#include <QuartzCore/QuartzCore.h>

Hope this helps

Tim

like image 65
Tim Davies Avatar answered Nov 15 '22 21:11

Tim Davies


I think all that can be accomplished with:

self.stopButton.layer.cornerRadius = 12.0;

But did you try

self.stopButton.opaque = NO;
self.stopButton.backgroundColor = [UIColor clearColor];

?

like image 21
bshirley Avatar answered Nov 15 '22 19:11

bshirley