Skip to content

Instantly share code, notes, and snippets.

@ranmyfriend
Created December 5, 2017 01:04
Show Gist options
  • Save ranmyfriend/e2976dc14862178ca27c07620bd0551d to your computer and use it in GitHub Desktop.
Save ranmyfriend/e2976dc14862178ca27c07620bd0551d to your computer and use it in GitHub Desktop.
Rounded ImgeView with nice borderline
//
// RoundedView.swift
// RoundedImageView
//
// Created by Ranjith Kumar on 12/5/17.
// Copyright © 2017 Dash. All rights reserved.
//
import Foundation
import UIKit
//@note:Recommended Size: CGSize(width:70,height:70)
struct Attributes {
let borderWidth:CGFloat = 3.0
let borderColor:UIColor = UIColor.white
let backgroundColor:UIColor = UIColor.red
let size:CGSize = CGSize(width:68,height:68)
}
class RoundedView:UIView {
private var attributes:Attributes?
private lazy var imageView:UIImageView = {
let iv = UIImageView()
iv.layer.borderWidth = (attributes?.borderWidth)!
iv.layer.borderColor = attributes?.borderColor.cgColor
iv.clipsToBounds = true
return iv
}()
init(frame: CGRect,attributes:Attributes) {
self.attributes = attributes
super.init(frame: frame)
clipsToBounds = true
backgroundColor = attributes.backgroundColor
addSubview(imageView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
clipsToBounds = true
backgroundColor = attributes?.backgroundColor
addSubview(imageView)
}
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = frame.height/2
imageView.frame = CGRect(x:1,y:1,width:(attributes?.size.width)!,height:(attributes?.size.height)!)
imageView.layer.cornerRadius = imageView.frame.height/2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment