It's important to point out that if you define your shape's position as (0,0), the bottom corner of the border will be placed in the center of the image, that's why I set it to: (frameSize.width/2,frameSize.height/2)
I then use my method to get the dashed border using the UIImage of my UIImageView and add the CAShapeLayer as a sublayer of the UIImageView layer:
You can also set different types of design using pattern image like below example.
[yourView.layer setBorderWidth:5.0];
[yourView.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DotedImage.png"]] CGColor]];///just add image name and create image with dashed or doted drawing and add here
Here you've to add <QuartzCore/QuartzCore> framework in the project and import it with below line in YourViewController.m file.
Swift 3:
Use in a storyboard (as custom class) or directly in code:
Result:
Building upon what Prasad G has suggested I created a method inside a UIImage Extras class with the following:
It's important to point out that if you define your shape's position as (0,0), the bottom corner of the border will be placed in the center of the image, that's why I set it to: (frameSize.width/2,frameSize.height/2)
I then use my method to get the dashed border using the UIImage of my UIImageView and add the CAShapeLayer as a sublayer of the UIImageView layer:
You can set the border with this pattern using Layer and Bezier path like below examples.
Objective-C
Swift 3.1
You can also set different types of design using pattern image like below example.
Here you've to add
<QuartzCore/QuartzCore>
framework in the project and import it with below line inYourViewController.m
file.Swift version of the QuartzCore answer.
The
CAShapeLayer
approach works, but the QuartzCore approach is better at handling a Table View reload, if theUIView
is inside a cell.For the image, you can use something like this (it's really small):
I tend to prefer vector over PNGs when I can get away with it:
Images.xcassets
, create aNew Image Set
called dottedPatternScale Factors
toSingle Vector
For Xamarin.iOS dashed/dotted border.
This is if you wanted it in Swift 2