├── LICENSE ├── MultiSelectSegmentedControl.swift ├── MultiSelectSegmentedControl.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── mihael.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── MultiSelectSegmentedControl.xcscheme │ └── xcschememanagement.plist ├── MultiSelectSegmentedControl ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist └── ViewController.swift ├── README.md └── stuff └── example.gif /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Mihael Isaev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MultiSelectSegmentedControl.swift 3 | // MultiSelectSegmentedControl 4 | // 5 | // Created by Mihael Isaev on 12/03/2017. 6 | // Copyright © 2017 Mihael Isaev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | public protocol MultiSelectSegmentedControlDelegate { 12 | func multiSelect(multiSelectSegmendedControl: MultiSelectSegmentedControl, didChangeValue value: Bool, atIndex index: Int) 13 | } 14 | 15 | open class MultiSelectSegmentedControl: UISegmentedControl { 16 | open var delegate: MultiSelectSegmentedControlDelegate? 17 | 18 | fileprivate var selectedIndexes: IndexSet = IndexSet() 19 | open var selectedSegmentIndexes: IndexSet { 20 | get { 21 | return selectedIndexes 22 | } 23 | set { 24 | if newValue.count == 0 { 25 | selectedIndexes = newValue 26 | } else { 27 | var validIndexes = IndexSet() 28 | for index in newValue { 29 | if index < numberOfSegments { 30 | validIndexes.insert(index) 31 | } 32 | } 33 | selectedIndexes = validIndexes 34 | } 35 | selectSegmentsOfSelectedIndexes() 36 | } 37 | } 38 | open var selectedSegmentTitles: [String] { 39 | get { 40 | var titleArray: [String] = [] 41 | for index in selectedSegmentIndexes { 42 | titleArray.append(titleForSegment(at: index)!) 43 | } 44 | return titleArray 45 | } 46 | } 47 | open var hideSeparatorBetweenSelectedSegments = false { 48 | didSet { 49 | selectSegmentsOfSelectedIndexes() 50 | } 51 | } 52 | fileprivate var hasBeenDrawn = false 53 | fileprivate var sortedSegments: [UIView] = [] 54 | 55 | open func selectAllSegments(_ select: Bool) { 56 | selectedSegmentIndexes = select ? IndexSet(integersIn: 0...numberOfSegments) : IndexSet() 57 | } 58 | 59 | //MARK: Internals 60 | 61 | func initSortedSegmentsArray() { 62 | sortedSegments = subviews 63 | sortedSegments.sort { (o1, o2) -> Bool in 64 | let x1 = o1.frame.origin.x 65 | let x2 = o2.frame.origin.x 66 | return x1 < x2 67 | } 68 | } 69 | 70 | fileprivate func selectSegmentsOfSelectedIndexes() { 71 | super.selectedSegmentIndex = UISegmentedControlNoSegment 72 | var isPrevSelected = false 73 | for i in 0...numberOfSegments { 74 | let isSelected = selectedIndexes.contains(i) 75 | if sortedSegments.count > i { 76 | let control = sortedSegments[i] 77 | control.setValue(isSelected, forKey: "selected") 78 | if i > 0 { 79 | let showBuiltinDivider = (isSelected && isPrevSelected && !hideSeparatorBetweenSelectedSegments) ? false : true 80 | sortedSegments[i-1].setValue(showBuiltinDivider, forKey: "showDivider") 81 | } 82 | isPrevSelected = isSelected 83 | } 84 | } 85 | } 86 | 87 | internal func valueChanged() { 88 | let tappedSegmentIndex = super.selectedSegmentIndex 89 | if selectedIndexes.contains(tappedSegmentIndex) { 90 | selectedIndexes.remove(tappedSegmentIndex) 91 | delegate?.multiSelect(multiSelectSegmendedControl: self, didChangeValue: false, atIndex: tappedSegmentIndex) 92 | } else { 93 | selectedIndexes.insert(tappedSegmentIndex) 94 | delegate?.multiSelect(multiSelectSegmendedControl: self, didChangeValue: true, atIndex: tappedSegmentIndex) 95 | } 96 | selectSegmentsOfSelectedIndexes() 97 | } 98 | 99 | //MARK: Initialization 100 | 101 | fileprivate func onInit() { 102 | addTarget(self, action: #selector(valueChanged), for: .valueChanged) 103 | } 104 | 105 | override init(frame: CGRect) { 106 | super.init(frame: frame) 107 | onInit() 108 | } 109 | 110 | public required init?(coder aDecoder: NSCoder) { 111 | super.init(coder: aDecoder) 112 | onInit() 113 | } 114 | 115 | //MARK: Overrides 116 | 117 | open override func draw(_ rect: CGRect) { 118 | if !hasBeenDrawn { 119 | initSortedSegmentsArray() 120 | hasBeenDrawn = true 121 | selectSegmentsOfSelectedIndexes() 122 | } 123 | super.draw(rect) 124 | } 125 | 126 | open override var isMomentary: Bool { 127 | didSet { 128 | //working with momentary state 129 | } 130 | } 131 | 132 | open override var selectedSegmentIndex: Int { 133 | get { 134 | return selectedIndexes.count == 0 ? UISegmentedControlNoSegment : selectedIndexes.first! 135 | } 136 | set { 137 | if selectedIndexes.count == 0 { 138 | super.selectedSegmentIndex = newValue 139 | } 140 | selectedSegmentIndexes = newValue == UISegmentedControlNoSegment ? IndexSet() : IndexSet(integer: selectedSegmentIndex) 141 | } 142 | } 143 | 144 | fileprivate func onInsertSegmentAtIndex(segment: Int) { 145 | selectedIndexes.shift(startingAt: segment, by: 1) 146 | initSortedSegmentsArray() 147 | } 148 | 149 | open override func insertSegment(withTitle title: String?, at segment: Int, animated: Bool) { 150 | super.insertSegment(withTitle: title, at: segment, animated: animated) 151 | } 152 | 153 | open override func insertSegment(with image: UIImage?, at segment: Int, animated: Bool) { 154 | super.insertSegment(with: image, at: segment, animated: animated) 155 | onInsertSegmentAtIndex(segment: segment) 156 | } 157 | 158 | open override func removeSegment(at segment: Int, animated: Bool) { 159 | if numberOfSegments == 0 { 160 | return 161 | } 162 | var segment = segment 163 | if segment >= numberOfSegments { 164 | segment = numberOfSegments - 1 165 | } 166 | var newSelectedIndexes = selectedIndexes 167 | newSelectedIndexes.insert(segment) 168 | newSelectedIndexes.shift(startingAt: segment, by: -1) 169 | super.selectedSegmentIndex = segment 170 | super.removeSegment(at: segment, animated: animated) 171 | selectedIndexes = newSelectedIndexes 172 | let delayInSeconds: DispatchTime = DispatchTime.now() + (animated ? .milliseconds(450) : .seconds(0)) 173 | DispatchQueue.main.asyncAfter(deadline: delayInSeconds) { 174 | self.initSortedSegmentsArray() 175 | self.selectSegmentsOfSelectedIndexes() 176 | } 177 | } 178 | 179 | open override func removeAllSegments() { 180 | super.selectedSegmentIndex = 0 181 | super.removeAllSegments() 182 | selectedIndexes.removeAll() 183 | sortedSegments = [] 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 471DCD061E75ED6000CFA78D /* MultiSelectSegmentedControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 471DCD051E75ED6000CFA78D /* MultiSelectSegmentedControl.swift */; }; 11 | 4781510A1E75B1A1003F3085 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 478151091E75B1A1003F3085 /* AppDelegate.swift */; }; 12 | 4781510C1E75B1A1003F3085 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4781510B1E75B1A1003F3085 /* ViewController.swift */; }; 13 | 4781510F1E75B1A1003F3085 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4781510D1E75B1A1003F3085 /* Main.storyboard */; }; 14 | 478151111E75B1A1003F3085 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 478151101E75B1A1003F3085 /* Assets.xcassets */; }; 15 | 478151141E75B1A1003F3085 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 478151121E75B1A1003F3085 /* LaunchScreen.storyboard */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 471DCD051E75ED6000CFA78D /* MultiSelectSegmentedControl.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultiSelectSegmentedControl.swift; sourceTree = SOURCE_ROOT; }; 20 | 478151061E75B1A1003F3085 /* MultiSelectSegmentedControl.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MultiSelectSegmentedControl.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 478151091E75B1A1003F3085 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22 | 4781510B1E75B1A1003F3085 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 23 | 4781510E1E75B1A1003F3085 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 24 | 478151101E75B1A1003F3085 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | 478151131E75B1A1003F3085 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | 478151151E75B1A1003F3085 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | 478151031E75B1A1003F3085 /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | 478150FD1E75B1A1003F3085 = { 41 | isa = PBXGroup; 42 | children = ( 43 | 478151081E75B1A1003F3085 /* MultiSelectSegmentedControl */, 44 | 478151071E75B1A1003F3085 /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | 478151071E75B1A1003F3085 /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | 478151061E75B1A1003F3085 /* MultiSelectSegmentedControl.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | 478151081E75B1A1003F3085 /* MultiSelectSegmentedControl */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | 478151091E75B1A1003F3085 /* AppDelegate.swift */, 60 | 4781510B1E75B1A1003F3085 /* ViewController.swift */, 61 | 471DCD051E75ED6000CFA78D /* MultiSelectSegmentedControl.swift */, 62 | 4781510D1E75B1A1003F3085 /* Main.storyboard */, 63 | 478151101E75B1A1003F3085 /* Assets.xcassets */, 64 | 478151121E75B1A1003F3085 /* LaunchScreen.storyboard */, 65 | 478151151E75B1A1003F3085 /* Info.plist */, 66 | ); 67 | path = MultiSelectSegmentedControl; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | 478151051E75B1A1003F3085 /* MultiSelectSegmentedControl */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = 478151181E75B1A1003F3085 /* Build configuration list for PBXNativeTarget "MultiSelectSegmentedControl" */; 76 | buildPhases = ( 77 | 478151021E75B1A1003F3085 /* Sources */, 78 | 478151031E75B1A1003F3085 /* Frameworks */, 79 | 478151041E75B1A1003F3085 /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = MultiSelectSegmentedControl; 86 | productName = MultiSelectSegmentedControl; 87 | productReference = 478151061E75B1A1003F3085 /* MultiSelectSegmentedControl.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | 478150FE1E75B1A1003F3085 /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0820; 97 | LastUpgradeCheck = 0820; 98 | ORGANIZATIONNAME = "Mihael Isaev"; 99 | TargetAttributes = { 100 | 478151051E75B1A1003F3085 = { 101 | CreatedOnToolsVersion = 8.2.1; 102 | DevelopmentTeam = 94H38KUMB5; 103 | ProvisioningStyle = Automatic; 104 | }; 105 | }; 106 | }; 107 | buildConfigurationList = 478151011E75B1A1003F3085 /* Build configuration list for PBXProject "MultiSelectSegmentedControl" */; 108 | compatibilityVersion = "Xcode 3.2"; 109 | developmentRegion = English; 110 | hasScannedForEncodings = 0; 111 | knownRegions = ( 112 | en, 113 | Base, 114 | ); 115 | mainGroup = 478150FD1E75B1A1003F3085; 116 | productRefGroup = 478151071E75B1A1003F3085 /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | 478151051E75B1A1003F3085 /* MultiSelectSegmentedControl */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXResourcesBuildPhase section */ 126 | 478151041E75B1A1003F3085 /* Resources */ = { 127 | isa = PBXResourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | 478151141E75B1A1003F3085 /* LaunchScreen.storyboard in Resources */, 131 | 478151111E75B1A1003F3085 /* Assets.xcassets in Resources */, 132 | 4781510F1E75B1A1003F3085 /* Main.storyboard in Resources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXResourcesBuildPhase section */ 137 | 138 | /* Begin PBXSourcesBuildPhase section */ 139 | 478151021E75B1A1003F3085 /* Sources */ = { 140 | isa = PBXSourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | 4781510C1E75B1A1003F3085 /* ViewController.swift in Sources */, 144 | 471DCD061E75ED6000CFA78D /* MultiSelectSegmentedControl.swift in Sources */, 145 | 4781510A1E75B1A1003F3085 /* AppDelegate.swift in Sources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXSourcesBuildPhase section */ 150 | 151 | /* Begin PBXVariantGroup section */ 152 | 4781510D1E75B1A1003F3085 /* Main.storyboard */ = { 153 | isa = PBXVariantGroup; 154 | children = ( 155 | 4781510E1E75B1A1003F3085 /* Base */, 156 | ); 157 | name = Main.storyboard; 158 | sourceTree = ""; 159 | }; 160 | 478151121E75B1A1003F3085 /* LaunchScreen.storyboard */ = { 161 | isa = PBXVariantGroup; 162 | children = ( 163 | 478151131E75B1A1003F3085 /* Base */, 164 | ); 165 | name = LaunchScreen.storyboard; 166 | sourceTree = ""; 167 | }; 168 | /* End PBXVariantGroup section */ 169 | 170 | /* Begin XCBuildConfiguration section */ 171 | 478151161E75B1A1003F3085 /* Debug */ = { 172 | isa = XCBuildConfiguration; 173 | buildSettings = { 174 | ALWAYS_SEARCH_USER_PATHS = NO; 175 | CLANG_ANALYZER_NONNULL = YES; 176 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 177 | CLANG_CXX_LIBRARY = "libc++"; 178 | CLANG_ENABLE_MODULES = YES; 179 | CLANG_ENABLE_OBJC_ARC = YES; 180 | CLANG_WARN_BOOL_CONVERSION = YES; 181 | CLANG_WARN_CONSTANT_CONVERSION = YES; 182 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 183 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 184 | CLANG_WARN_EMPTY_BODY = YES; 185 | CLANG_WARN_ENUM_CONVERSION = YES; 186 | CLANG_WARN_INFINITE_RECURSION = YES; 187 | CLANG_WARN_INT_CONVERSION = YES; 188 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 189 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 193 | COPY_PHASE_STRIP = NO; 194 | DEBUG_INFORMATION_FORMAT = dwarf; 195 | ENABLE_STRICT_OBJC_MSGSEND = YES; 196 | ENABLE_TESTABILITY = YES; 197 | GCC_C_LANGUAGE_STANDARD = gnu99; 198 | GCC_DYNAMIC_NO_PIC = NO; 199 | GCC_NO_COMMON_BLOCKS = YES; 200 | GCC_OPTIMIZATION_LEVEL = 0; 201 | GCC_PREPROCESSOR_DEFINITIONS = ( 202 | "DEBUG=1", 203 | "$(inherited)", 204 | ); 205 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 206 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 207 | GCC_WARN_UNDECLARED_SELECTOR = YES; 208 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 209 | GCC_WARN_UNUSED_FUNCTION = YES; 210 | GCC_WARN_UNUSED_VARIABLE = YES; 211 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 212 | MTL_ENABLE_DEBUG_INFO = YES; 213 | ONLY_ACTIVE_ARCH = YES; 214 | SDKROOT = iphoneos; 215 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 216 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 217 | }; 218 | name = Debug; 219 | }; 220 | 478151171E75B1A1003F3085 /* Release */ = { 221 | isa = XCBuildConfiguration; 222 | buildSettings = { 223 | ALWAYS_SEARCH_USER_PATHS = NO; 224 | CLANG_ANALYZER_NONNULL = YES; 225 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 226 | CLANG_CXX_LIBRARY = "libc++"; 227 | CLANG_ENABLE_MODULES = YES; 228 | CLANG_ENABLE_OBJC_ARC = YES; 229 | CLANG_WARN_BOOL_CONVERSION = YES; 230 | CLANG_WARN_CONSTANT_CONVERSION = YES; 231 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 232 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 233 | CLANG_WARN_EMPTY_BODY = YES; 234 | CLANG_WARN_ENUM_CONVERSION = YES; 235 | CLANG_WARN_INFINITE_RECURSION = YES; 236 | CLANG_WARN_INT_CONVERSION = YES; 237 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 238 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 239 | CLANG_WARN_UNREACHABLE_CODE = YES; 240 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 241 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 242 | COPY_PHASE_STRIP = NO; 243 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 244 | ENABLE_NS_ASSERTIONS = NO; 245 | ENABLE_STRICT_OBJC_MSGSEND = YES; 246 | GCC_C_LANGUAGE_STANDARD = gnu99; 247 | GCC_NO_COMMON_BLOCKS = YES; 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 255 | MTL_ENABLE_DEBUG_INFO = NO; 256 | SDKROOT = iphoneos; 257 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 258 | VALIDATE_PRODUCT = YES; 259 | }; 260 | name = Release; 261 | }; 262 | 478151191E75B1A1003F3085 /* Debug */ = { 263 | isa = XCBuildConfiguration; 264 | buildSettings = { 265 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 266 | DEVELOPMENT_TEAM = 94H38KUMB5; 267 | INFOPLIST_FILE = MultiSelectSegmentedControl/Info.plist; 268 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 269 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 270 | PRODUCT_BUNDLE_IDENTIFIER = com.mihaelisaev.MultiSelectSegmentedControl; 271 | PRODUCT_NAME = "$(TARGET_NAME)"; 272 | SWIFT_VERSION = 3.0; 273 | }; 274 | name = Debug; 275 | }; 276 | 4781511A1E75B1A1003F3085 /* Release */ = { 277 | isa = XCBuildConfiguration; 278 | buildSettings = { 279 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 280 | DEVELOPMENT_TEAM = 94H38KUMB5; 281 | INFOPLIST_FILE = MultiSelectSegmentedControl/Info.plist; 282 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 283 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 284 | PRODUCT_BUNDLE_IDENTIFIER = com.mihaelisaev.MultiSelectSegmentedControl; 285 | PRODUCT_NAME = "$(TARGET_NAME)"; 286 | SWIFT_VERSION = 3.0; 287 | }; 288 | name = Release; 289 | }; 290 | /* End XCBuildConfiguration section */ 291 | 292 | /* Begin XCConfigurationList section */ 293 | 478151011E75B1A1003F3085 /* Build configuration list for PBXProject "MultiSelectSegmentedControl" */ = { 294 | isa = XCConfigurationList; 295 | buildConfigurations = ( 296 | 478151161E75B1A1003F3085 /* Debug */, 297 | 478151171E75B1A1003F3085 /* Release */, 298 | ); 299 | defaultConfigurationIsVisible = 0; 300 | defaultConfigurationName = Release; 301 | }; 302 | 478151181E75B1A1003F3085 /* Build configuration list for PBXNativeTarget "MultiSelectSegmentedControl" */ = { 303 | isa = XCConfigurationList; 304 | buildConfigurations = ( 305 | 478151191E75B1A1003F3085 /* Debug */, 306 | 4781511A1E75B1A1003F3085 /* Release */, 307 | ); 308 | defaultConfigurationIsVisible = 0; 309 | defaultConfigurationName = Release; 310 | }; 311 | /* End XCConfigurationList section */ 312 | }; 313 | rootObject = 478150FE1E75B1A1003F3085 /* Project object */; 314 | } 315 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl.xcodeproj/xcuserdata/mihael.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl.xcodeproj/xcuserdata/mihael.xcuserdatad/xcschemes/MultiSelectSegmentedControl.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl.xcodeproj/xcuserdata/mihael.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MultiSelectSegmentedControl.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 478151051E75B1A1003F3085 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // MultiSelectSegmentedControl 4 | // 5 | // Created by Mihael Isaev on 12/03/2017. 6 | // Copyright © 2017 Mihael Isaev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /MultiSelectSegmentedControl/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 43 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /MultiSelectSegmentedControl/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // MultiSelectSegmentedControl 4 | // 5 | // Created by Mihael Isaev on 12/03/2017. 6 | // Copyright © 2017 Mihael Isaev. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | class ViewController: UIViewController, MultiSelectSegmentedControlDelegate { 12 | 13 | @IBOutlet weak var days: MultiSelectSegmentedControl! 14 | @IBOutlet weak var simple: MultiSelectSegmentedControl! 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | days.delegate = self 19 | simple.delegate = self 20 | days.selectedSegmentIndexes = IndexSet(integersIn: 1...2) 21 | days.selectedSegmentIndexes.insert(4) 22 | simple.selectedSegmentIndexes = IndexSet(integer: 2) 23 | } 24 | 25 | @IBAction func selectAll() { 26 | days.selectAllSegments(true) 27 | simple.selectAllSegments(true) 28 | } 29 | 30 | @IBAction func selectNone() { 31 | days.selectAllSegments(false) 32 | simple.selectAllSegments(false) 33 | } 34 | 35 | func multiSelect(multiSelectSegmendedControl: MultiSelectSegmentedControl, didChangeValue value: Bool, atIndex index: Int) { 36 | print("delegate value: \(value) atIndex: \(index)") 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Release version](https://img.shields.io/badge/release-1.0-green.svg)]() 2 | 3 | # Deprecated 4 | 5 | Take a look at my new awesome [UIKitPlus](https://github.com/MihaelIsaev/UIKitPlus) lib which will give you an ability to build this kind of control in a minute. 6 | 7 | ### Swift 3.0.1 8 | 9 | # MultiSelectSegmentedControl 10 | *multiple selection segmented control rewritten from [yonat Obj-C lib](https://github.com/yonat/MultiSelectSegmentedControl)* 11 | 12 | ![Example](stuff/example.gif) 13 | 14 | A subclass of UISegmentedControl that supports selection multiple segments. 15 | 16 | No need for images - works with the builtin styles of UISegmentedControl. 17 | 18 | ## Installation 19 | 20 | Simply Copy and Paste `MultiSelectSegmentedControl.swift` file in your Xcode Project :) 21 | 22 | ## Usage 23 | 24 | Drag a `UISegmentedControl` into your view in Interface Builder. 25 | 26 | Set its class to `MultiSelectSegmentedControl`. 27 | 28 | Set an outlet for it, perhaps calling it something creative such as `myMultiSeg`. 29 | 30 | Set the selected segments: 31 | ``` swift 32 | //to select 1 and 2 segments 33 | myMultiSeg.selectedSegmentIndexes = IndexSet(integersIn: 1...2) 34 | //to select 4 segment in addition to 1 and 2 35 | myMultiSeg.selectedSegmentIndexes.insert(4) 36 | //to select just 2 segment 37 | myMultiSeg.selectedSegmentIndexes = IndexSet(integer: 2) 38 | ``` 39 | 40 | Get the selected segment indices: 41 | ``` swift 42 | let selectedIndices: IndexSet = myMultiSeg.selectedSegmentIndexes 43 | ``` 44 | 45 | Get the selected segment titles: 46 | ``` oswiftbjc 47 | print("These items are selected: \(myMultiSeg.selectedSegmentTitles)") 48 | ``` 49 | 50 | If you want to be notified of changes to the control's value, make sure your ViewController conforms to the delegate protocol 51 | 52 | ```swift 53 | class YourAwesomeViewController: UIViewController, MultiSelectSegmentedControlDelegate { 54 | ``` 55 | 56 | ...and set the delegate, perhaps in your `viewDidLoad` method: 57 | 58 | ``` swift 59 | myMultiSeg.delegate = self 60 | ``` 61 | 62 | You are notified of changes through the following method: 63 | ``` swift 64 | func multiSelect(multiSelectSegmendedControl: MultiSelectSegmentedControl, didChangeValue value: Bool, atIndex index: Int) { 65 | print("multiSelect delegate selected: \(value) atIndex: \(index)") 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /stuff/example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MihaelIsaev/MultiSelectSegmentedControl/188ec86375439f73613dcc3ea1b5529c53059cb9/stuff/example.gif --------------------------------------------------------------------------------