I have a UIButton which is very similar to the standard iOS keyboard alphabet button.
I am not sure how to create a shadow only for the bottom layer like how iOS have done.
I use the below code, but I see a shadow on all the side, not just the bottom:
CALayer *buttonLayer = [[CALayer alloc] init];
buttonLayer.shadowColor = [UIColor grayColor].CGColor;
buttonLayer.shadowOffset = CGSizeMake(0.f,1.f);
buttonLayer.masksToBounds = NO;
buttonLayer.shadowOpacity = 1.f;
How can I achieve the same effect?
You can mix the cornerRadius and shadow properties. I tested it on iOS 8.
Objective-C:
[self.globeButton setImage:[UIImage imageNamed:#"Globe"] forState:UIControlStateNormal];
self.globeButton.backgroundColor = [UIColor colorWithRed:171 green:178 blue:186 alpha:1.0f];
// Shadow and Radius
self.globeButton.layer.shadowColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.25f] CGColor];
self.globeButton.layer.shadowOffset = CGSizeMake(0, 2.0f);
self.globeButton.layer.shadowOpacity = 1.0f;
self.globeButton.layer.shadowRadius = 0.0f;
self.globeButton.layer.masksToBounds = NO;
self.globeButton.layer.cornerRadius = 4.0f;
Swift:
globeButton.setImage(UIImage(named: "Globe"), for: .normal)
globeButton.backgroundColor = UIColor(red: 171/255, green: 178/255, blue: 186/255, alpha: 1.0)
// Shadow Color and Radius
globeButton.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
globeButton.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
globeButton.layer.shadowOpacity = 1.0
globeButton.layer.shadowRadius = 0.0
globeButton.layer.masksToBounds = false
globeButton.layer.cornerRadius = 4.0
Result:
SWIFT 3
import UIKit
class ButtonWithShadow: UIButton {
override func draw(_ rect: CGRect) {
updateLayerProperties()
}
func updateLayerProperties() {
self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
self.layer.shadowOffset = CGSize(width: 0, height: 3)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 10.0
self.layer.masksToBounds = false
}
}
!! Only if you do not need corner radius and shadow simultaneously. Otherwise watch this.
Be sure to also set shadowRadius to 0:
Given a UIButton property named button set its backing layer properties like this:
self.button.layer.shadowColor = [UIColor grayColor].CGColor;
self.button.layer.shadowOffset = CGSizeMake(0, 1.0);
self.button.layer.shadowOpacity = 1.0;
self.button.layer.shadowRadius = 0.0;
I have created IBInspectable extension that will help you.
You can directly assign from storyboard
Swift 5
//MARK:- IBInspectable
extension UIView {
#IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
#IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
#IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
#IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.masksToBounds = false
layer.shadowRadius = newValue
}
}
#IBInspectable
var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.masksToBounds = false
layer.shadowOpacity = newValue
}
}
#IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.masksToBounds = false
layer.shadowOffset = newValue
}
}
#IBInspectable
var shadowColor: UIColor? {
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.shadowColor = color.cgColor
} else {
layer.shadowColor = nil
}
}
}
}
You can try with this code:
(sorry, i only know swift, not obj c. this code will add bottom shadow on your button.
button.layer.masksToBounds = false
button.layer.shadowColor = UIColor(rgb: 0x000000, alpha: 1.0).CGColor
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 0
button.layer.shadowOffset = CGSizeMake(0, 1.0)
View bottom shadow
swift 4.2
viewTop.layer.shadowOffset = CGSize(width: 0, height: 1)
viewTop.layer.shadowColor = UIColor.lightGray.cgColor
viewTop.layer.shadowOpacity = 1
viewTop.layer.shadowRadius = 5
viewTop.layer.masksToBounds = false
in swift 2.0 add shadow uiview (uibutton) with code before class declaration or in swift file functions:
extension UIView {
func addShadowView(width:CGFloat=0.2, height:CGFloat=0.2, Opacidade:Float=0.7, maskToBounds:Bool=false, radius:CGFloat=0.5){
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSize(width: width, height: height)
self.layer.shadowRadius = radius
self.layer.shadowOpacity = Opacidade
self.layer.masksToBounds = maskToBounds
}
}
in viewdidload add this code
button.addShadowView()
CornerRadius and shadow don't mix well on the same layer. So you will have to embed your "to be cornered" UIButton inside an UIView. The shadow will be applied on this UIView.
The following code, given as an example, produces the image below it:
import UIKit
class CustomButton: UIButton {
required init(coder decoder: NSCoder) {
super.init(coder: decoder)
backgroundColor = UIColor.whiteColor()
}
override func drawRect(rect: CGRect) {
updateLayerProperties()
}
func updateLayerProperties() {
layer.masksToBounds = true
layer.cornerRadius = 12.0
//superview is your optional embedding UIView
if let superview = superview {
superview.backgroundColor = UIColor.clearColor()
superview.layer.shadowColor = UIColor.darkGrayColor().CGColor
superview.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: 12.0).CGPath
superview.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
superview.layer.shadowOpacity = 1.0
superview.layer.shadowRadius = 2
superview.layer.masksToBounds = true
superview.clipsToBounds = false
}
}
}
Put this method into your UIView extension and play with offset value
func drawShadow(shadowColor: UIColor = UIColor.black, opacity: Float =
0.3, offset: CGSize, radius: CGFloat = 5, shouldRasterize : Bool = false) {
self.layer.shadowColor = shadowColor.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowOffset = offset
self.layer.shadowRadius = radius
self.layer.shouldRasterize = shouldRasterize
}
To add shadow to a button with corner radius:
final class CustomButton: UIButton {
private var shadowLayer: CAShapeLayer!
override func layoutSubviews() {
super.layoutSubviews()
if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 33).cgPath
if self.backgroundColor != nil {
shadowLayer.fillColor = self.backgroundColor?.cgColor
}
else{
shadowLayer.fillColor = UIColor.white.cgColor
}
shadowLayer.shadowColor = UIColor.gray.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 0.0, height: 3.0)
shadowLayer.shadowOpacity = 0.4
shadowLayer.shadowRadius = 2
layer.insertSublayer(shadowLayer, at: 0)
}
}
}
You can try with this code:
LoveButtonOutlet.layer.backgroundColor = UIColor.white.cgColor
LoveButtonOutlet.layer.cornerRadius = 10
LoveButtonOutlet.layer.borderWidth = 2
LoveButtonOutlet.layer.borderColor = UIColor.black.cgColor
LoveButtonOutlet.layer.shadowOffset = CGSize(width: 0, height: 1)
LoveButtonOutlet.layer.shadowColor = UIColor.darkGray.cgColor
LoveButtonOutlet.layer.shadowOpacity = 1
LoveButtonOutlet.layer.shadowRadius = 5
LoveButtonOutlet.layer.masksToBounds = false
extension UIButton
{
func setButtonCornerRadiusOnly(getValue:CGFloat)
{
self.layer.cornerRadius = getValue
self.clipsToBounds = true
}
func setButtonBorderColorAndHeight(getHeight:CGFloat,getColor:UIColor)
{
self.layer.borderColor = getColor.cgColor
self.layer.borderWidth = getHeight
}
func setBtnWithShadow()
{
self.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)
self.layer.shadowOpacity = 0.5
self.layer.shadowRadius = 1.5
self.layer.cornerRadius = self.frame.size.height/2
self.layer.masksToBounds = false
}
}
Related
I want to add rounded corners to top left and top right and shadow around it except the bottom. There should be no shadow at bottom.
I am able to achieve rounded corners as needed. But not shadow. I need a code where both is achieved . In below image look at the Diagnose view. It has top left and top right corner rounded and shadow around it.
Try This Code.
Custom Method. Swift 4.0
#IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
customCornerWithShadow(myView: myView, cornerRadius: 20)
}
func customCornerWithShadow(myView:UIView,cornerRadius:CGFloat) {
myView.layer.cornerRadius = cornerRadius
myView.clipsToBounds = false[enter image description here][1]
myView.layer.maskedCorners = CACornerMask(rawValue: CACornerMask.RawValue(UInt8(CACornerMask.layerMinXMinYCorner.rawValue) | UInt8(CACornerMask.layerMaxXMinYCorner.rawValue)))
myView.layer.shadowColor = #colorLiteral(red: 0.3098039329, green: 0.01568627544, blue: 0.1294117719, alpha: 1)
myView.layer.shadowOffset = CGSize(width:0, height:-1)
let shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: cornerRadius)
myView.layer.shadowPath = shadowPath.cgPath
myView.layer.shadowOpacity = 1
}
#IBOutlet var btnActive : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// btnActive.backgroundColor = UIColor(red: 171, green: 178, blue: 186, alpha: 1.0)
// Shadow and Radius
btnActive.layer.shadowColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1).cgColor
btnActive.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
btnActive.layer.shadowOpacity = 2.0
btnActive.layer.shadowRadius = 5.0
btnActive.layer.masksToBounds = false
btnActive.layer.cornerRadius = 4.0
}
Found the solution :
clipsToBounds = true, makes corners round but shadow gets hidden.
so we can use 2 views one as container and the main view as its subview . Container will have clipstobound = false and the main view will have clipstobounds true.
let containerV = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width , height: 50))
containerV.clipsToBounds = false
containerV.layer.shadowColor = UIColor.black.cgColor
containerV.layer.shadowOpacity = 1
containerV.layer.shadowOffset = CGSize(width: -0.5, height: -3.0)
/* since we dont want shadow at bottom */
containerV.layer.shadowRadius = 2
containerV.layer.shadowPath = UIBezierPath(roundedRect: containerV.bounds,
cornerRadius: 10).cgPath
let headerLabel = UILabel()
headerLabel.frame = containerV.bounds
headerLabel.text = "Test Header"
headerLabel.backgroundColor = UIColor.white
headerLabel.clipsToBounds = true
headerLabel.layer.cornerRadius = 10
if #available(iOS 11.0, *) {
headerLabel.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
} else {
// Fallback on earlier versions
}
containerV.addSubview(headerLabel)
You Can Use extension,
in Swift 4.1 and Xcode 9.3.1
extension UIView {
#IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
}
}
#IBInspectable
var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
#IBInspectable
var borderColor: UIColor? {
get {
if let color = layer.borderColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.borderColor = color.cgColor
} else {
layer.borderColor = nil
}
}
}
#IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
#IBInspectable
var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}
#IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
#IBInspectable
var shadowColor: UIColor? {
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.shadowColor = color.cgColor
} else {
layer.shadowColor = nil
}
}
}
}
I want to add shadow(it is not like default it somehow clouded and blurred) for my UIView like below
I have written an extension as
func addShadow(color: UIColor = UIColor.black, opacity: Float = 0.9, radius: CGFloat = 1, scale: Bool = true) {
self.layer.masksToBounds = false
self.layer.shadowColor = color.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowRadius = radius
self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
self.layer.shouldRasterize = true
self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
Output:
but not able to get the exact output.
Your help will be thankful.
I believe that you also need to set clipsToBounds.
self.clipsToBounds = false
I use IBInspectable to treat all the views in my app. Try it out
extension UIView {
#IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
}
get {
return layer.cornerRadius
}
}
#IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
#IBInspectable var borderColor: UIColor? {
set {
layer.borderColor = newValue?.cgColor
}
get {
return UIColor(cgColor: layer.borderColor!)
}
}
#IBInspectable var shadowOffset: CGSize {
set {
layer.shadowOffset = newValue
}
get {
return layer.shadowOffset
}
}
#IBInspectable var shadowOpacity: Float {
set {
layer.shadowOpacity = newValue
}
get {
return layer.shadowOpacity
}
}
#IBInspectable var shadowRadius: CGFloat {
set {
layer.shadowRadius = newValue
}
get {
return layer.shadowRadius
}
}
#IBInspectable var shadowColor: UIColor? {
set {
layer.shadowColor = newValue?.cgColor
}
get {
return UIColor(cgColor: layer.shadowColor!)
}
}
}
in your case you should fix shadowOffset property.
You should disable clipToBounds in your 'containerView' (view that has your shadow dropping view placed as a subview)
Take a look at an example:
import UIKit
extension UIView {
func addShadow(color: UIColor = UIColor.black, opacity: Float = 0.9, radius: CGFloat = 1, scale: Bool = true) {
self.layer.masksToBounds = false
self.layer.shadowColor = color.cgColor
self.layer.shadowOpacity = opacity
self.layer.shadowRadius = radius
self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
self.layer.shouldRasterize = true
self.layer.rasterizationScale = scale ? UIScreen.main.scale : 1
}
}
let shadowedView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
shadowedView.backgroundColor = .blue
shadowedView.layer.cornerRadius = 15.0
shadowedView.addShadow()
let mainView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height:200))
mainView.backgroundColor = .white
mainView.addSubview(shadowedView)
here I will add a view container and enable clipToBounds :
let shadowedView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
shadowedView.backgroundColor = .blue
shadowedView.layer.cornerRadius = 15.0
shadowedView.addShadow()
let containerView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 100))
containerView.addSubview(shadowedView)
containerView.clipsToBounds = true
let mainView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
mainView.backgroundColor = .white
mainView.addSubview(containerView)
I tried to give a shadow to label text but it does not shown.
My code
private func drawValueLabel() {
valueLabel.layer.shadowColor = UIColor.red.cgColor
valueLabel.layer.shadowOffset = CGSize.init(width: 15.0, height: 15.0)
valueLabel.layer.shadowRadius = 3.0
valueLabel.layer.shadowOpacity = 1
valueLabel.layer.masksToBounds = false
valueLabel.clipsToBounds = false
valueLabel.layer.shouldRasterize = true
valueLabel.drawText(in: self.bounds)
}
Help me to show shadow
Thanks
I'm using this UIView extension for add shadow. With this extension you can add shadow from storyboard.
extension UIView {
#IBInspectable var shadow: Bool {
get {
return layer.shadowOpacity > 0.0
}
set {
if newValue == true {
self.addShadow()
}
}
}
func addShadow(shadowColor: CGColor = UIColor.black.cgColor,
shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0),
shadowOpacity: Float = 0.4,
shadowRadius: CGFloat = 3.0) {
layer.shadowColor = shadowColor
layer.shadowOffset = shadowOffset
layer.shadowOpacity = shadowOpacity
layer.shadowRadius = shadowRadius
}
}
Your code is working fine for me. Try giving some smaller offset so that you would be able to see it like
label.layer.shadowOffset = CGSize.init(width: 3.0, height: 3.0)
Use this -
label.layer.shadowColor = UIColor.black.cgColor
label.layer.shadowOpacity = 0.5
label.layer.shadowRadius = 2.0
label.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
In my xib file, I have created a generic function through which I wanted to add a bottom border through implementing a shadow to those textfields
func setupTextField(textField: UITextField) {
textField.leftViewMode = UITextFieldViewMode.always
let imageView = UIImageView();
let image = UIImage(named: "calendar");
imageView.image = image;
imageView.frame = CGRect(x: 5, y: 8, width: 20, height: 20)
textField.addSubview(imageView)
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: textField.frame.height))
textField.leftView = paddingView
textField.borderStyle = .none
textField.layer.backgroundColor = UIColor.white.cgColor
textField.layer.masksToBounds = false
textField.layer.shadowColor = UIColor(hex: "#D8D8D8").cgColor
textField.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
textField.layer.shadowOpacity = 1.0
textField.layer.shadowRadius = 0.0
}
And I have called this function from my awakeFromNib() method and as a result it added the shadow on my 2nd UITextField but not at the 1st one. Though from debug I can see that if there is no 2nd UITextField then this would draw a shadow on my 1st one.
How to overcome this problem?
For swift 4, swift 4.2, swift 5 Please refer below answer
Can give shadow to any UIView sub classes(imageview, label, textfield, textview etc)
extension UIView {
/* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */
#IBInspectable var shadowColor: UIColor? {
set {
layer.shadowColor = newValue!.cgColor
}
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
else {
return nil
}
}
}
/* The opacity of the shadow. Defaults to 0.4 Specifying a value outside the
* [0,1] range will give undefined results. Animatable. */
#IBInspectable var shadowOpacity: Float {
set {
layer.shadowOpacity = newValue
}
get {
return layer.shadowOpacity
}
}
/* The shadow offset. Defaults to (1, 2). Animatable. */
#IBInspectable var shadowOffset: CGPoint {
set {
layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
}
get {
return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
}
}
/* The blur radius used to create the shadow. Defaults to 3. Animatable. */
#IBInspectable var shadowRadius: CGFloat {
set {
layer.shadowRadius = newValue
}
get {
return layer.shadowRadius
}
}
}
Textfield shadow
swift 4.2
textFieldDOB.backgroundColor = UIColor.white textFieldDOB.clipsToBounds = false
textFieldDOB.layer.shadowColor = UIColor.black.cgColor
textFieldDOB.layer.shadowOffset = CGSize.zero
textFieldDOB.layer.shadowOpacity = 0.3
textFieldDOB.layer.shadowRadius = 6
textFieldDOB.borderStyle = .none
I am trying to set the custom border color of UIView programmatically in Swift.
If you Use Swift 2.0+
self.yourView.layer.borderWidth = 1
self.yourView.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
In Swift 4 you can set the border color and width to UIControls using below code.
let yourColor : UIColor = UIColor( red: 0.7, green: 0.3, blue:0.1, alpha: 1.0 )
yourControl.layer.masksToBounds = true
yourControl.layer.borderColor = yourColor.CGColor
yourControl.layer.borderWidth = 1.0
< Swift 4, You can set UIView's border width and border color using the below code.
yourView.layer.borderWidth = 1
yourView.layer.borderColor = UIColor.red.cgColor
Use #IBDesignable and #IBInspectable to do the same.
They are re-useable, easily modifiable from the Interface Builder and the changes are reflected immediately in the Storyboard
Conform the objects in the storyboard to the particular class
Code Snippet:
#IBDesignable
class CustomView: UIView{
#IBInspectable var borderWidth: CGFloat = 0.0{
didSet{
self.layer.borderWidth = borderWidth
}
}
#IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
}
Allows easy modification from Interface Builder:
You can write an extension to use it with all the UIViews eg. UIButton, UILabel, UIImageView etc.
You can customise my following method as per your requirement, but I think it will work well for you.
extension UIView{
func setBorder(radius:CGFloat, color:UIColor = UIColor.clearColor()) -> UIView{
var roundView:UIView = self
roundView.layer.cornerRadius = CGFloat(radius)
roundView.layer.borderWidth = 1
roundView.layer.borderColor = color.CGColor
roundView.clipsToBounds = true
return roundView
}
}
Usage:
btnLogin.setBorder(7, color: UIColor.lightGrayColor())
imgViewUserPick.setBorder(10)
Swift 5.2, UIView+Extension
extension UIView {
public func addViewBorder(borderColor:CGColor,borderWith:CGFloat,borderCornerRadius:CGFloat){
self.layer.borderWidth = borderWith
self.layer.borderColor = borderColor
self.layer.cornerRadius = borderCornerRadius
}
}
You used this extension;
yourView.addViewBorder(borderColor: #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1), borderWith: 1.0, borderCornerRadius: 20)
swift 3
func borderColor(){
self.viewMenuItems.layer.cornerRadius = 13
self.viewMenuItems.layer.borderWidth = 1
self.viewMenuItems.layer.borderColor = UIColor.white.cgColor
}
We can create method for it. Simply use it.
public func createBorderForView(color: UIColor, radius: CGFloat, width: CGFloat = 0.7) {
self.layer.borderWidth = width
self.layer.cornerRadius = radius
self.layer.shouldRasterize = false
self.layer.rasterizationScale = 2
self.clipsToBounds = true
self.layer.masksToBounds = true
let cgColor: CGColor = color.cgColor
self.layer.borderColor = cgColor
}
Swift 3.0
groundTrump.layer.borderColor = UIColor.red.cgColor
Write the code in your viewDidLoad()
self.view.layer.borderColor = anyColor().CGColor
And you can set Color with RGB
func anyColor() -> UIColor {
return UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 1.0)
}
Learn something about CALayer in UIKit
swift 3.0
self.uiTextView.layer.borderWidth = 0.5
self.txtItemShortDes.layer.borderColor = UIColor(red:205.0/255.0, green:205.0/255.0, blue:205.0/255.0, alpha: 1.0).cgColor
Swift 5*
I, always use view extension to make view corners round, set border color and width and it has been the most convenient way for me. just copy and paste this code and controlle these properties in attribute inspector.
extension UIView {
#IBInspectable
var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
}
}
#IBInspectable
var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
#IBInspectable
var borderColor: UIColor? {
get {
if let color = layer.borderColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.borderColor = color.cgColor
} else {
layer.borderColor = nil
}
}
}
}
if you are dealing with the layers you can give border like this....
if not sometime you might have to true the wantslayer like self.anytextField.wantslayer = true
var BackgroundColor : CGColor = UIColor.blue.cgColor
subLayer.shadowColor = self.BackgroundColor
subLayer.borderColor = self.BackgroundColor
subLayer.borderWidth = CGFloat(4)
or you can use the extension of CA Layer for macOS
func addBorder(edge: NSRectEdge, color: NSColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case NSRectEdge.maxY:
border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
case NSRectEdge.minY:
border.frame = CGRect(x:0, y: frame.height - thickness, width: frame.width, height:thickness)
case NSRectEdge.minX:
border.frame = CGRect(x:0, y:0, width: thickness, height: frame.height)
case NSRectEdge.maxX:
border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: frame.height)
default: do {}
}
border.backgroundColor = color.cgColor
addSublayer(border)
}
its usage is given below
subLayer.addBorder(edge: <#T##NSRectEdge#>, color: <#T##NSColor#>, thickness: <#T##CGFloat#>)