Custom UIImageView Border

Related searches

I'm trying to make a border like this:

With this code:

self.slot1.layer.cornerRadius = self.slot1.bounds.height / 2
self.slot1.layer.borderWidth = 1.5
self.slot1.layer.borderColor = UIColor.orange.cgColor 

It produces:

How can i add a "spacing" between the border and the actual image?

You can achieve this using below extension

extension UIImage {
    func imageWithInsets(insets: UIEdgeInsets) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(
            CGSize(width: self.size.width + insets.left + insets.right,
                   height: self.size.height + insets.top + insets.bottom), false, self.scale)
        let _ = UIGraphicsGetCurrentContext()
        let origin = CGPoint(x: insets.left, y: insets.top)
        self.draw(at: origin)
        let imageWithInsets = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return imageWithInsets
    }
}

Use below code to add space between image and border, i add here 20 you can give your own space

self.slot1.layer.cornerRadius = self.slot1.bounds.height / 2
self.slot1.layer.borderWidth = 1.5
self.slot1.layer.borderColor = UIColor.orange.cgColor
let image = self.slot1.image?.imageWithInsets(insets: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
self.slot1.image = image

Hope this help!

How to add a border outline color to a UIView, All UIView subclasses have a built-in way to draw a border around them using their underlying CALayer . This function gets the original image, calculates a new rectangle (the original minus the border width) in which the new image should fill in, scales down the image and returns the scaled image. Adding the border in the image can be done with the code below. This snippet is pretty self explanatory.

Instead of trying to make space in between, take one UIView inside that put your UIImageView. UIImageView should have less width and height, then add corner radius to both but add border color to UIVIew only.

Your UI should be like this :

Swift: Adding a Border, Corner Radius, and Shadow to a UIView, My Swift tool belt includes an extension that will add a border, corner The extension we wrote for UIView will also extend our custom controls. // Define a new image object UIImage * image = [UIImage imageNamed: @ "lake.png"]; // Define a new image view UIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (10, 10, 157, 125)]; // Set image in imageview and add to view [imgView setImage: image]; [[self view] addSubview: imgView]; // Create a white border with defined width imgView.layer.borderColor = [UIColor whiteColor].CGColor; imgView.layer.borderWidth = 1.5; // Set image corner radius imgView.layer.cornerRadius

Run in playground

import PlaygroundSupport
import UIKit

let iv = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 200, height: 200))
PlaygroundPage.current.liveView = iv

iv.backgroundColor = .blue
iv.layer.cornerRadius = 100
iv.layer.borderColor = UIColor.red.cgColor
iv.layer.borderWidth = 5
iv.layer.sublayers?.count

let layer2 = CAShapeLayer()
let newFrame = iv.bounds.insetBy(dx: 7, dy: 7)
layer2.path = UIBezierPath(roundedRect: newFrame, cornerRadius: newFrame.height / 2).cgPath
layer2.frame = iv.bounds
layer2.lineWidth = 5
layer2.strokeColor = UIColor.yellow.cgColor
layer2.fillColor = UIColor.clear.cgColor
iv.layer.addSublayer(layer2)

How can i take an UIImage and give it a black border?, How to set the custom border color of UIView programmatically , In Swift 4 you can set the border color and width to UIControls using below code. < Swift 4, You � WASHINGTON, DC – U. S. Customs and Border Protection (CBP) today announced the public launch of a border wall system webpage that features construction video and an interactive map. The border wall system webpage will give the American people a first-hand look at its construction and the border wall system’s impact on our national security.

UIImageView Rounded Corners in Swift, To make the image border and visible I will set the borderWidth and the borderColor. myImageView.layer.borderWidth =� U.S. Customs and Border Protection Please click and zoom on the map to see more information about the Border Wall System

iOS Tutorial: Add border, shadow, corner radius for UIView and , Add border, shadow, corner radius for UIView and UIImageView is very iOS Tutorials Duration: 10:35 Posted: Dec 8, 2018 In interface builder take an UIView and set the custom class as RSUIImageViewMultiborder. RSUIImageViewCustomBadge will be available through cocoapod soon. Customizing. Adding a new border is the most easiest part. To add a new border in RSUIImageViewCustomBadge.h add two property of the following format

U.S. Customs and Border Protection . Contact: Contact Customs and Border Protection . Check Airport Wait Times. Local Offices: Ports of Entry—Air, Land, and Sea. Main Address: 1300 Pennsylvania Ave., NW Washington, DC 20229. Phone Number: 1-202-325-8000. Toll Free: 1-877-CBP-5511 (1-877-227-5511) TTY: 1-866-880-6582. Forms: Customs and Border

Comments
  • Just embed it in UIView make rounded corners of view give border and color
  • Thank you @Sharad, do you have a code to achieve this?
  • Achieve what ? Do you have that Imageview in Storyboard or nib ? Just make changes there, put UIVIew in place of ImageView and place ImageView inside the UIView. Then simply use your code on both of them for cornerRadius.