- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Border for an ImageView in iOS?
Setting border for image view is easy, In this post we will see how to set Border for an Image View in iOS.
Let’s get started.
Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “BorderToImage”
We will create an image view and a button in our storyboard on tap of button we will add border to image view. We could do the same in viewDidLoad but to see the difference we’re doing this.
Step 2 − In Main.storyboard add an image view and a button as shown below.
Step 3 − Create @IBOutlet for image and name it imgView and create for button and name it btnAddBorder.
Step 4 − Add below code in btnAddBorder function
@IBAction func btnAddBorder(_ sender: Any) { imgView.layer.borderColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).cgColor imgView.layer.masksToBounds = true imgView.contentMode = .scaleToFill imgView.layer.borderWidth = 5 }
And you’re done run the code to see output.
Example
import UIKit class ViewController: UIViewController { @IBOutlet var imgView: UIImageView! override func viewDidLoad() { super.viewDidLoad() } @IBAction func btnAddBorder(_ sender: Any) { imgView.layer.borderColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).cgColor imgView.layer.masksToBounds = true imgView.contentMode = .scaleToFill imgView.layer.borderWidth = 5 } }
Output
- Related Articles
- How can I set a Border for an ImageView in Android?
- How to rotate an image in imageview by an angle on iOS App using Swift?
- How can I set an icon for my iOS application?
- How to Set opacity for View in iOS?
- How to set ImageView in edittext?
- How to overlay two images in Android to set an ImageView?
- How to set background for Navigation Bar in iOS?
- Set dashed line for border with CSS
- Set dotted line for border with CSS
- How to overlay two images in Android to set an imageview using Kotlin?
- How to set border color for SoftBevelBorder in Java?
- Set a dark border to an element in Bootstrap
- Set a blue border to an element in Bootstrap
- Set a white border to an element in Bootstrap
- How can I set an ImageView's width and height programmatically in Android?

Advertisements