The missing UIKit component. A scrollable alternative to UISegmentedControl

Related tags

Layout UITabControl
Overview

Platform: iOS 15.0+ Language: Swift 5 License: MIT

UITabControl-Expo

Requirements

iOS 15.0 and higher

Installation

Swift Package Manager:

dependencies: [
  .package(url: "https://github.com/hugo-pivaral/UITabControl.git", .exact("1.0.0")),
],

After installing the SPM into your project import UITabControl with

import UITabControl

Usage

If you're not using storyboards, jut use the init(tabs: [String]) method to create a new UITabControl instance, set the views frame and add it to your superview. Please note that UITabControl has an intrinsic content size value of 48, so make sure to set the height value or constraint to 48.

let tabControl = UITabControl(tabs: ["All", "Business", "Technology", "Science", "Politics", "Health"])
tabControl.frame = CGRect(x: x, y: y, width: width, height: 48)

self.view.addSubview(tabControl)

If you are using the interface builder in your project, add a UIView to your view controller scene or xib file, and make it inherit from UITabControl. Again, remember to set a height constraint with a constant value of 48.

example

In your swift file, configure your @IBOutlet like this.

@IBOutlet weak var tabControl: UITabControl! {
    didSet {
        tabControl.setTabs(["All", "Business", "Technology", "Science", "Politics", "Health"])
    }
}

UITabControl inherits from UIControl, so to handle touch events you can use the addTarget(_:action:for:) method, and set the value for controlEvents to .valueChanged.

tabControl.addTarget(self, action: #selector(handleEvents(_:)), for: .valueChanged)

Finally, add your @objc method.

@objc func handleEvents(_ sender: UITabControl) {
    let selectedIndex = sender.selectedTabIndex
    // Do something...
}

Customization

To customize UITabControl, create a new instance of UITabControl.Appearance with your custom values, and assign it in the init(tabs: [String], appearance: Appearance) method, or using the .setAppearance(_) method.

let appearance = UITabControl.Appearance(style: UITabControl.Style,
                                         textColor: UIColor,
                                         contentInset: CGFloat,
                                         separatorColor: UIColor,
                                         separatorEnabled: Bool,
                                         selectedTabTextColor: UIColor,
                                         selectedTabShadowEnabled: Bool,
                                         selectedTabBackgroundColor: UIColor)
tabControl.setAppearance(appearance)

styles

Author

Hugo Pivaral

License

UITabControl is under the MIT license. See LICENSE for details.

You might also like...
Apple provides us two ways to use UIKit views in SwiftUI

RepresentableKit Apple provides us two ways to use UIKit views in SwiftUI: UIVie

Alura Viagens with UIKit
Alura Viagens with UIKit

Alura Viagens UIKit UITableView Protocolo UITableViewDelegate Constraints para d

A simple checklist app using UIKit
A simple checklist app using UIKit

UIKit Checklist App Data Persistence Icon Selector Notification Scheduling Multi

PillboxView - A Pillbox informational view for iOS written in UIKit
PillboxView - A Pillbox informational view for iOS written in UIKit

Pillbox View Pillbox View shows a small bubble, pill looking box that sides from

LemonadeDeclarativeUI framework contains some new functions for UIKit

LemonadeDeclarativeUI framework contains some new functions for UIKit. This library has been developing. If you want to contribute reach me!

UIKitTest - Xcode, UIKit and Swift playground

UIKit Test Just a Xcode, UIKit and Swift playground (try and learning purposes).

Swift-picker-views - inline single and multi picker views for UIKit. Without tableview! Easy and simple
Swift-picker-views - inline single and multi picker views for UIKit. Without tableview! Easy and simple

swift-picker-views Inline single and multiple picker views for UIKit. No tablevi

A Set of Tools To Extend UIKit (Classic iOS Framework)
A Set of Tools To Extend UIKit (Classic iOS Framework)

RVS_UIKit_Toolbox A set of basic UIKit tools, for Swift iOS app development. Overview This package offers a few extensions of standard UIKit classes,

UIKit Practice Project – Simple app to store names along with photos of people you've met
UIKit Practice Project – Simple app to store names along with photos of people you've met

People UIKit Practice Project #10 – Simple app to store names along with photos of people you've met Cool Features Light & dark mode support Responsiv

Owner
Hugo Pivaral
iOS & Web developer. Passionate for creating amazing digital products & experiences.
Hugo Pivaral
Using the UIKitChain framework, You can create a UIKit component in one line of code.

Using the UIKitChain framework, You can create a UIKit component in one line of code. Installation CocoaPods CocoaPods is a dependency manager for Coc

Malith Nadeeshan 1 Sep 1, 2022
Controls-Practice-UIKit- - Controls Practice (UIKit)

Controls Practice (UIKit) Change a number 0 to 255 different ways: Button (+1) I

null 1 Feb 13, 2022
An alternative layout system for iOS

GranadaLayout GranadaLayout is an alternative layout system for iOS, inspired on the Android layout system. It includes relative and linear layout sys

Jose Alcalá Correa 49 Mar 5, 2022
Component containing supplementary content that are anchored to the bottom of the screen.

BottomSheetController UIKit component containing supplementary content that are anchored to the bottom of the screen. Requirements Installation Swift

Naruki Chigira 37 Nov 15, 2022
UITableView based component designed to display a hierarchy of expandable/foldable comments.

SwiftyComments UITableView based component designed to display a hierarchy of expandable/foldable comments. Installation Manually Just copy the .swift

Stéphane Sercu 220 Dec 22, 2022
Alignment Control is a component to align top, middle, bottom, left, center, right.

AlignmentControl is a component for alignment Direction AlignmentDirection Horizontal Vertical Mode AlignmentMode Left Center Right Top Middle Bottom

Beslan Tularov 38 Mar 16, 2022
CompositionalLayoutDSL, library to simplify the creation of UICollectionViewCompositionalLayout. It wraps the UIKit API and makes the code shorter and easier to read.

CompositionalLayoutDSL CompositionalLayoutDSL is a Swift library. It makes easier to create compositional layout for collection view. Requirements Doc

FABERNOVEL 44 Dec 27, 2022
A declarative UIKit for improve layout productivity when developing an iOS application

TifoKit A declarative UIKit for improve layout productivity when developing an iOS application Requirements Min. iOS 11 Swift 5+ Installation Currentl

Tifo Audi A.P 22 Aug 9, 2022
🌵Declarative UIKit

Viewtiful Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installation Viewtifu

Yongjun Lee 2 Oct 28, 2021
Pocket Trainer UIKit

Pocket-Trainer-UIKit Помощник для занятий в тренажерном зале API для взаимодействия с базой данных упражнений и первоначальное приложение на Android з

vladukha 0 Nov 28, 2021