3 Essential Swift Extension Codes You Should Know

Kerem DEMİR
4 min readSep 14, 2023

--

1-) Adding Subviews to UIViews Quickly and Readably in Swift

# addSubViews Method

In Swift programming, adding subviews to UIView objects is a common operation. Especially when you need to add a significant number of subviews, you’ll want to find ways to make your code cleaner and more readable. This is where a special Swift extension called addSubViews comes into play.

Code Introduction

import UIKit

extension UIView {
func addSubViews(_ views: UIView...){
views.forEach({
addSubview($0)
})
}
}

The above code snippet adds a new function to the UIView class. This function can accept a variable number of UIView objects and adds each of them as a subview to the current UIView object. This makes adding subviews quick and straightforward.

How to Use

Here’s an example illustrating how to use this custom extension :

let parentView = UIView()
let button = UIButton()
let label = UILabel()
let textField = UITextField()

parentView.addSubViews(button, label, view)

The code above uses the addSubViews function to add button, label, and textField as subviews to a parentView.

This Swift extension simplifies the process of adding subviews between UIView objects, making your code more readable and efficient. It helps you keep your code clean and saves time when you need to add multiple subviews.

2-) Automatically Hiding the Keyboard

# hideKeyboardWhenTappedAround Method

Introduction

iOS applications often involve scenarios where users need to open the keyboard to input data. However, expecting users to manually dismiss the keyboard every time they’re done typing can be a frustrating user experience. In this article, iOS developers will learn how to automatically hide the keyboard using a code snippet.

The Code Snippet

The following code snippet extends any UIViewController class and allows the keyboard to be automatically hidden when the user taps anywhere on the screen.

import UIKit

extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
view.endEditing(true)
}
}

How to Use

In any UIViewController subclass or an extended class where you want to use it, call the hideKeyboardWhenTappedAround() function. For example:

class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// Call the function to automatically hide the keyboard
hideKeyboardWhenTappedAround()
}

// Add the rest of your ViewController code here
}

Conclusion

This simple code snippet allows you to automatically hide the keyboard in your iOS applications, improving the user experience and making it more user-friendly. Users will no longer need to perform additional actions to dismiss the keyboard while filling out forms or entering data.

3-) Creating Stylish Circular Buttons in iOS Apps with Swift

# makeCircular Method

Introduction

Buttons are considered a fundamental component of an iOS application’s user interaction. However, sometimes, you may want to use more eye-catching and aesthetically pleasing buttons instead of standard rectangular ones. In this article, you’ll learn a straightforward way to create circular buttons using Swift. Circular buttons provide an excellent way to enhance the user experience and make your app’s design more appealing.

The Code Snippet

Here is the Swift code snippet that you can use to create circular buttons :

import UIKit

extension UIButton {
func makeCircular(cornerRadius: CGFloat, borderWidth: CGFloat, borderColor: UIColor) {
self.layer.cornerRadius = cornerRadius
self.clipsToBounds = true
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.cgColor
}
}

How to Use

Using this code is quite simple, and all you need to do is follow these steps:

  1. Add the Code to Your Project : First, you need to add the code snippet to an appropriate location in your project.
  2. Customize the Button : Create a UIButton instance or select an existing button.
  3. Use the Code : To make the button circular, utilize the makeCircular(cornerRadius: borderWidth: borderColor:) method. Here's an example :
let myButton = UIButton() // Create a UIButton instance or select an existing button
myButton.makeCircular(cornerRadius: 20.0, borderWidth: 2.0, borderColor: UIColor.blue)

This code will make your button circular and allow you to optionally set the corner radius, border width, and border color.

Conclusion

This simple Swift code snippet offers a quick and effective way to improve your iOS app’s design and enhance the user experience. Circular buttons are perfect for attracting user attention and enhancing the aesthetic appearance of your application. Add it to your project now and start using it!

I hope these custom Swift extensions make your work easier and help you better organize your code. Keep following me on Medium for more Swift tips and tricks!

Follow me on GitHub : https://github.com/keremdemirios

Follow me on LinkedIn : https://www.linkedin.com/in/kerem-demir-b31a69190/

--

--

No responses yet