Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a triangle shape in a UIButton

I am trying to create a button with a triangle icon/shape in it. I have created the triangle in Paintcode then copied the beizer path and added it to the button layer as below.

-(void)setupShowHideRouteButton {
    UIBezierPath* bezierPath = UIBezierPath.bezierPath;
    [bezierPath moveToPoint: CGPointMake(10.5, 8.5)];
    [bezierPath addCurveToPoint: CGPointMake(40.5, 8.5) controlPoint1: CGPointMake(39.5, 8.5) controlPoint2: CGPointMake(40.5, 8.5)];
    [bezierPath addLineToPoint: CGPointMake(26.39, 22.3)];
    [bezierPath addLineToPoint: CGPointMake(25.2, 23.5)];
    [bezierPath addLineToPoint: CGPointMake(10.5, 8.5)];
    [UIColor.blackColor setStroke];
    bezierPath.lineWidth = 1;
    [bezierPath stroke];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.showHideRouteViewBtn.bounds;
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    shapeLayer.lineWidth = 2;
    [self.showHideRouteViewBtn.layer addSublayer:shapeLayer];
}

However, this does not appear to work. I am setting the background color of the UIButton so I know the frame is correct and outlet working as expected it is just not adding the shape?

Second Approach

UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(10.5, 8.5)];
[bezierPath addCurveToPoint: CGPointMake(40.5, 8.5) controlPoint1: CGPointMake(39.5, 8.5) controlPoint2: CGPointMake(40.5, 8.5)];
[bezierPath addLineToPoint: CGPointMake(26.39, 22.3)];
[bezierPath addLineToPoint: CGPointMake(25.2, 23.5)];
[bezierPath addLineToPoint: CGPointMake(10.5, 8.5)];
[UIColor.blackColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];

// Create a mask layer and the frame to determine what will be visible in the view.
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = self.showHideRouteViewBtn.bounds;

// Create a path with the rectangle in it.
CGPathRef path = CGPathCreateWithRect(maskRect, NULL);

// Set the path to the mask layer.
maskLayer.path = bezierPath.CGPath;

// Release the path since it's not covered by ARC.
CGPathRelease(path);

// Set the mask of the view.
self.showHideRouteViewBtn.layer.mask = maskLayer;

This did not work either.

like image 200
StuartM Avatar asked Jul 15 '14 22:07

StuartM


People also ask

How do I create a button from a UIButton?

The parent can be a Figure created using the uifigure function, or one of its child containers. btn = uibutton (parent,style) creates a Button of the specified style in the specified parent container. btn = uibutton ( ___,Name,Value) creates a Button with properties specified by one or more Name,Value pair arguments.

What is UIButton in MATLAB?

btn = uibutton creates a push button in a new figure and returns the Button object. MATLAB ® calls the uifigure function to create the figure.

How to create a triangle on a web page?

The easiest way to create a triangle on a web page is by simply drawing images and using that images on your web page. You can either use javascript to draw the triangle on canvas or use photoshop to create a triangle image. In this article we will learn how to create the triangle using CSS.

How to make a rounded triangle in illustrator?

After you use one of the methods above to make a triangle. Use the Direct Selection Tool ( A) to select the triangle. Click on the little circle near the corners and drag it toward the center to make a rounded triangle. How to distort a triangle in Illustrator?


1 Answers

Try the following:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = self.showHideRouteViewBtn.bounds;
shapeLayer.path = bezierPath.CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
shapeLayer.lineWidth = 2;
self.showHideRouteViewBtn.layer.mask = shapeLayer;
like image 97
klcjr89 Avatar answered Sep 20 '22 14:09

klcjr89