• Stars
    star
    298
  • Rank 134,487 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 11 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

UISegmentedControl remake that supports selecting multiple segments, vertical stacking, combining text and images.

MultiSelectSegmentedControl

Swift Version Build Status License CocoaPods Compatible
Platform PRs Welcome

UISegmentedControl remake that supports selecting multiple segments, vertical stacking, combining text and images.

Features

  • Single or multiple selection.
  • Horizontal or vertical stacking.
  • Can show text and images together.
  • Use from either storyboard or code.
  • UIAppearance support.

Usage

Very similar to UISegmentedControl, can be used as a drop-in replacement in most cases.

If you use Interface Builder, add a regular UIView and then set its class to MultiSelectSegmentedControl.

SwiftUI Usage

MultiSegmentPicker(
    selectedSegmentIndexes: $indexSet,
    items: ["One", "Two", image, [image2, "Text"], "Last"]
)

The properties mentioned below can be passed as arguments to the MultiSegmentPicker initializer, or used as view modifiers (e.g., .borderWidth(3)).

Creating Segments

Each segment can contain an image, a text, or both:

let multiSelect = MultiSelectSegmentedControl()
multiSelect.items = ["One", "Two", image, [image2, "Text"], "Last"]

Images are shown in full color (unlike UISegmentedControl). To make them render in the same tintColor as the control, use template mode:

multiSelect.items = [image1, image2, image3].map { $0.withRenderingMode(.alwaysTemplate) }

Selecting Segments

multiSelect.selectedSegmentIndexes = [1, 2, 4]

Or just single selection:

multiSelect.allowsMultipleSelection = false
multiSelect.selectedSegmentIndex = 3

Getting Selected Segments

let selectedIndices: IndexSet = multiSelect.selectedSegmentIndexes

Or to get the titles:

let titles: [String] = multiSelect.selectedSegmentTitles

Handling User Selection Changes

You can use standard target-action:

multiSelect.addTarget(self, action: #selector(selectionChanged), for: .valueChanged)

Or conform to the delegate protocol:

extension MyViewController: MultiSelectSegmentedControlDelegate {
    func multiSelect(_ multiSelectSegmentedControl: MultiSelectSegmentedControl, didChange value: Bool, at index: Int) {
        print("selected \(value) at \(index)")
    }
}

... and set the delegate:

multiSelect.delegate = self

Changing Appearance

Color:

multiSelect.tintColor = .green

Background Color (optional - use if background color should be different from tint color):

multiSelect.selectedBackgroundColor = .blue

Shape:

multiSelect.borderWidth = 3 // Width of the dividers between segments and the border around the view.
multiSelect.borderRadius = 32 // Corner radius of the view.

Stack the segments vertically:

multiSelect.isVertical = true

Stack each segment contents vertically when it contains both image and text:

multiSelect.isVerticalSegmentContents = true

Text styling:

multiSelect.setTitleTextAttributes([.foregroundColor: UIColor.yellow], for: .selected)
multiSelect.setTitleTextAttributes([.obliqueness: 0.25], for: .normal)

More label styling:

multiSelect.titleConfigurationHandler = {
    $0.numberOfLines = 0
    $0.lineBreakMode = .byWordWrapping
}

Installation

CocoaPods:

pod 'MultiSelectSegmentedControl'

Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/yonat/MultiSelectSegmentedControl", from: "2.3.9")
]

TODO

  • foreground color of selected segment should be/appear transparent
  • configure segment layoutMargins, stackView.spacing

Meta

@yonatsharon

https://github.com/yonat/MultiSelectSegmentedControl

More Repositories

1

MultiSlider

UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizontal.
Swift
436
star
2

StepProgressView

Step-by-step progress view with labels and shapes. A good replacement for UIActivityIndicatorView and UIProgressView.
Swift
365
star
3

RadioGroup

The missing iOS radio buttons group.
Swift
191
star
4

SelectionList

Simple single-selection or multiple-selection checklist, based on UITableView
Swift
116
star
5

MultiToggleButton

Multiple state tap-to-toggle UIButton (like old camera flash button)
Swift
83
star
6

CheckmarkCollectionViewCell

UICollectionViewCell with checkbox when it isSelected and empty circle when not - like Photos.app "Select" mode.
Swift
64
star
7

CameraBackground

Show camera layer as a background to any UIView
Swift
63
star
8

BatteryView

Simple battery shaped UIView
Swift
52
star
9

BadgeLabel

Simple animatable badge, based on UILabel and using CALayer
Objective-C
49
star
10

MockImagePicker

Mock UIImagePickerController for testing camera based UI in simulator
Swift
22
star
11

ContactsChangeNotifier

Which contacts changed outside your iOS app? Better CNContactStoreDidChange notification: Get real changes, without the noise.
Swift
14
star
12

EditableList

Simple List of UITextField-s based on UITableViewController
Objective-C
13
star
13

SweeterSwift

Extensions and syntactic sugar to enrich the Swift standard library, iOS frameworks, and SwifterSwift.
Swift
11
star
14

YONAutoComplete

Add auto-completion to a UITextField
Objective-C
9
star
15

MiniLayout

Minimal AutoLayout convenience layer. Program constraints succinctly. ⚠️ Moved into https://github.com/yonat/SweeterSwift
Swift
8
star
16

AvailableHapticFeedback

Unified haptic feedback interface for all subclasses of UIFeedbackGenerator, with availability checks for iOS 9.
Swift
8
star
17

EasyBackgroundRefresh

Easy background refresh registration, scheduling, execution, and completion. BGTaskScheduler for the lazy.
Swift
8
star
18

SwiftQuality

Common configuration files for SwiftLint and SwiftFormat
Ruby
5
star
19

smart_ptr

Simple C++ Smart Pointer Classes
C
3
star
20

versionit

Simple bash script to change Xcode project version and build number, create git tag, and update podspec.
Shell
3
star
21

pointainer

STL Extensions for containers of pointers
C++
2
star
22

Blinker

mac app that reminds you to blink by displaying animated blinking eyes on your screen at regular intervals
Swift
2
star
23

DirectoryUploader

Automatically upload all files saved in source directory, and then delete the uploaded files.
Swift
1
star