
- iOS Tutorial
- iOS - Home
- iOS - Getting Started
- iOS - Environment Setup
- iOS - Objective-C Basics
- iOS - First iPhone Application
- iOS - Actions and Outlets
- iOS - Delegates
- iOS - UI Elements
- iOS - Accelerometer
- iOS - Universal Applications
- iOS - Camera Management
- iOS - Location Handling
- iOS - SQLite Database
- iOS - Sending Email
- iOS - Audio & Video
- iOS - File Handling
- iOS - Accessing Maps
- iOS - In-App Purchase
- iOS - iAd Integration
- iOS - GameKit
- iOS - Storyboards
- iOS - Auto Layouts
- iOS - Twitter & Facebook
- iOS - Memory Management
- iOS - Application Debugging
- iOS Useful Resources
- iOS - Quick Guide
- iOS - Useful Resources
- iOS - Discussion
How to create a Border, Border radius, and shadow to a UIView in iPhone/iOS?
In this article we’ll learn how to create borders and shadows. We’ll do it in two ways, one by directly coding and one by making it designable and an extension of UIView, which can be edited directly in the storyboard.
Let’s see how to play around with borders in ios −
Method 1 − Creating borders with simple coding –
Borders are a property of layer, above which a View is drawn, a border has the following properties, Border color, border width.
self.view.layer.borderColor = colorLiteral(red: 0.4392156899, green: 0.01176470611, blue: 0.1921568662, alpha: 1) self.view.layer.borderWidth = 5.0
To create corner radius of a view we can use
self.view.layer.cornerRadius = 5
Here is the result of above code when run on simulator.
To create shadow we can use other properties like shadowPath, shadowColor, shadowOffSet, shadowOpactiy and shadowRadius.
Method 2 − Using Designable we can make these properties editable from the storyboard. Let’s see an example of playing around with borders using designables.
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 } } } }
This will create actions in the storyboard’s Attribute Inspector from where you can directly edit and access the results. It should look like the one below.
- Related Articles
- How do I draw a shadow under a UIView in iPhone/iOS?
- How to create a WebView in iOS/iPhone?
- How to add a shadow and a border on circular imageView android?
- How To Change UIView's Border Color And Thickness in Cocoa Touch?
- How to create a collapsed border in HTML?
- How to add a border to the top and bottom of an iOS View?
- How to create a border pane using JavaFX?
- How to create a compound border for a component in Java?
- How to create titled border for a Panel in Java?
- How to create Titled Border for a component in Java?
- How to create table border in HTML?
- How to create Border Images in CSS
- How to take a screenshot programmatically in iPhone/iOS?
- How to create a border with a raised beveled edge in Java?
- How to create a border with a lowered beveled edge in Java?
