├── Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif ├── README.md ├── SearchBar.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── xcuserdata │ └── Uwe.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── SearchBar ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj └── LaunchScreen.storyboard ├── ContentView.swift ├── Info.plist ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json └── SceneDelegate.swift /Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UPetersen/SwiftUI-SearchBar/0a8a358d7b685576c763dc28f32bf2c73c46fbb1/Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftUI searchBar 2 | 3 | Sample swiftUI project for a search bar realized completely in swiftUI used as [answer](https://stackoverflow.com/questions/56490963/how-to-display-a-search-bar-with-swiftui/58473985#58473985) to [this stackoverflow question](https://stackoverflow.com/questions/56490963/how-to-display-a-search-bar-with-swiftui) 4 | 5 | The search bar incorporates 6 | 7 | - a magnifying glas icon, 8 | - a clear button, 9 | - a cancel button, 10 | - resigning keyboard on dragging in the list and 11 | - hiding the navigation view when the search text field is selected. 12 | 13 | 14 | ![In action](https://github.com/UPetersen/SwiftUI-SearchBar/blob/master/Bildschirmvideo%20aufnehmen%202019-10-19%20um%2021.19.55_55.gif) 15 | -------------------------------------------------------------------------------- /SearchBar.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C60A7EC3235CB78C0082535C /* Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif in Resources */ = {isa = PBXBuildFile; fileRef = C60A7EC2235CB78C0082535C /* Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif */; }; 11 | C649F0442351F99000E23D98 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C649F0432351F99000E23D98 /* AppDelegate.swift */; }; 12 | C649F0462351F99000E23D98 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C649F0452351F99000E23D98 /* SceneDelegate.swift */; }; 13 | C649F0482351F99000E23D98 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C649F0472351F99000E23D98 /* ContentView.swift */; }; 14 | C649F04A2351F99500E23D98 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C649F0492351F99500E23D98 /* Assets.xcassets */; }; 15 | C649F04D2351F99500E23D98 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C649F04C2351F99500E23D98 /* Preview Assets.xcassets */; }; 16 | C649F0502351F99500E23D98 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C649F04E2351F99500E23D98 /* LaunchScreen.storyboard */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | C60A7EC1235CB6210082535C /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 21 | C60A7EC2235CB78C0082535C /* Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = "Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif"; sourceTree = ""; }; 22 | C649F0402351F99000E23D98 /* SearchBar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SearchBar.app; sourceTree = BUILT_PRODUCTS_DIR; }; 23 | C649F0432351F99000E23D98 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 24 | C649F0452351F99000E23D98 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 25 | C649F0472351F99000E23D98 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 26 | C649F0492351F99500E23D98 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 27 | C649F04C2351F99500E23D98 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 28 | C649F04F2351F99500E23D98 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 29 | C649F0512351F99500E23D98 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 30 | /* End PBXFileReference section */ 31 | 32 | /* Begin PBXFrameworksBuildPhase section */ 33 | C649F03D2351F99000E23D98 /* Frameworks */ = { 34 | isa = PBXFrameworksBuildPhase; 35 | buildActionMask = 2147483647; 36 | files = ( 37 | ); 38 | runOnlyForDeploymentPostprocessing = 0; 39 | }; 40 | /* End PBXFrameworksBuildPhase section */ 41 | 42 | /* Begin PBXGroup section */ 43 | C649F0372351F99000E23D98 = { 44 | isa = PBXGroup; 45 | children = ( 46 | C60A7EC1235CB6210082535C /* README.md */, 47 | C60A7EC2235CB78C0082535C /* Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif */, 48 | C649F0422351F99000E23D98 /* SearchBar */, 49 | C649F0412351F99000E23D98 /* Products */, 50 | ); 51 | sourceTree = ""; 52 | }; 53 | C649F0412351F99000E23D98 /* Products */ = { 54 | isa = PBXGroup; 55 | children = ( 56 | C649F0402351F99000E23D98 /* SearchBar.app */, 57 | ); 58 | name = Products; 59 | sourceTree = ""; 60 | }; 61 | C649F0422351F99000E23D98 /* SearchBar */ = { 62 | isa = PBXGroup; 63 | children = ( 64 | C649F0432351F99000E23D98 /* AppDelegate.swift */, 65 | C649F0452351F99000E23D98 /* SceneDelegate.swift */, 66 | C649F0472351F99000E23D98 /* ContentView.swift */, 67 | C649F0492351F99500E23D98 /* Assets.xcassets */, 68 | C649F04E2351F99500E23D98 /* LaunchScreen.storyboard */, 69 | C649F0512351F99500E23D98 /* Info.plist */, 70 | C649F04B2351F99500E23D98 /* Preview Content */, 71 | ); 72 | path = SearchBar; 73 | sourceTree = ""; 74 | }; 75 | C649F04B2351F99500E23D98 /* Preview Content */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | C649F04C2351F99500E23D98 /* Preview Assets.xcassets */, 79 | ); 80 | path = "Preview Content"; 81 | sourceTree = ""; 82 | }; 83 | /* End PBXGroup section */ 84 | 85 | /* Begin PBXNativeTarget section */ 86 | C649F03F2351F99000E23D98 /* SearchBar */ = { 87 | isa = PBXNativeTarget; 88 | buildConfigurationList = C649F0542351F99500E23D98 /* Build configuration list for PBXNativeTarget "SearchBar" */; 89 | buildPhases = ( 90 | C649F03C2351F99000E23D98 /* Sources */, 91 | C649F03D2351F99000E23D98 /* Frameworks */, 92 | C649F03E2351F99000E23D98 /* Resources */, 93 | ); 94 | buildRules = ( 95 | ); 96 | dependencies = ( 97 | ); 98 | name = SearchBar; 99 | productName = SearchBar; 100 | productReference = C649F0402351F99000E23D98 /* SearchBar.app */; 101 | productType = "com.apple.product-type.application"; 102 | }; 103 | /* End PBXNativeTarget section */ 104 | 105 | /* Begin PBXProject section */ 106 | C649F0382351F99000E23D98 /* Project object */ = { 107 | isa = PBXProject; 108 | attributes = { 109 | LastSwiftUpdateCheck = 1110; 110 | LastUpgradeCheck = 1110; 111 | ORGANIZATIONNAME = "Uwe Petersen"; 112 | TargetAttributes = { 113 | C649F03F2351F99000E23D98 = { 114 | CreatedOnToolsVersion = 11.1; 115 | }; 116 | }; 117 | }; 118 | buildConfigurationList = C649F03B2351F99000E23D98 /* Build configuration list for PBXProject "SearchBar" */; 119 | compatibilityVersion = "Xcode 9.3"; 120 | developmentRegion = en; 121 | hasScannedForEncodings = 0; 122 | knownRegions = ( 123 | en, 124 | Base, 125 | ); 126 | mainGroup = C649F0372351F99000E23D98; 127 | productRefGroup = C649F0412351F99000E23D98 /* Products */; 128 | projectDirPath = ""; 129 | projectRoot = ""; 130 | targets = ( 131 | C649F03F2351F99000E23D98 /* SearchBar */, 132 | ); 133 | }; 134 | /* End PBXProject section */ 135 | 136 | /* Begin PBXResourcesBuildPhase section */ 137 | C649F03E2351F99000E23D98 /* Resources */ = { 138 | isa = PBXResourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | C649F0502351F99500E23D98 /* LaunchScreen.storyboard in Resources */, 142 | C60A7EC3235CB78C0082535C /* Bildschirmvideo aufnehmen 2019-10-19 um 21.19.55_55.gif in Resources */, 143 | C649F04D2351F99500E23D98 /* Preview Assets.xcassets in Resources */, 144 | C649F04A2351F99500E23D98 /* Assets.xcassets in Resources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXResourcesBuildPhase section */ 149 | 150 | /* Begin PBXSourcesBuildPhase section */ 151 | C649F03C2351F99000E23D98 /* Sources */ = { 152 | isa = PBXSourcesBuildPhase; 153 | buildActionMask = 2147483647; 154 | files = ( 155 | C649F0442351F99000E23D98 /* AppDelegate.swift in Sources */, 156 | C649F0462351F99000E23D98 /* SceneDelegate.swift in Sources */, 157 | C649F0482351F99000E23D98 /* ContentView.swift in Sources */, 158 | ); 159 | runOnlyForDeploymentPostprocessing = 0; 160 | }; 161 | /* End PBXSourcesBuildPhase section */ 162 | 163 | /* Begin PBXVariantGroup section */ 164 | C649F04E2351F99500E23D98 /* LaunchScreen.storyboard */ = { 165 | isa = PBXVariantGroup; 166 | children = ( 167 | C649F04F2351F99500E23D98 /* Base */, 168 | ); 169 | name = LaunchScreen.storyboard; 170 | sourceTree = ""; 171 | }; 172 | /* End PBXVariantGroup section */ 173 | 174 | /* Begin XCBuildConfiguration section */ 175 | C649F0522351F99500E23D98 /* Debug */ = { 176 | isa = XCBuildConfiguration; 177 | buildSettings = { 178 | ALWAYS_SEARCH_USER_PATHS = NO; 179 | CLANG_ANALYZER_NONNULL = YES; 180 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 181 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 182 | CLANG_CXX_LIBRARY = "libc++"; 183 | CLANG_ENABLE_MODULES = YES; 184 | CLANG_ENABLE_OBJC_ARC = YES; 185 | CLANG_ENABLE_OBJC_WEAK = YES; 186 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 187 | CLANG_WARN_BOOL_CONVERSION = YES; 188 | CLANG_WARN_COMMA = YES; 189 | CLANG_WARN_CONSTANT_CONVERSION = YES; 190 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 191 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 192 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 193 | CLANG_WARN_EMPTY_BODY = YES; 194 | CLANG_WARN_ENUM_CONVERSION = YES; 195 | CLANG_WARN_INFINITE_RECURSION = YES; 196 | CLANG_WARN_INT_CONVERSION = YES; 197 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 198 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 199 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 200 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 201 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 202 | CLANG_WARN_STRICT_PROTOTYPES = YES; 203 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 204 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 205 | CLANG_WARN_UNREACHABLE_CODE = YES; 206 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 207 | COPY_PHASE_STRIP = NO; 208 | DEBUG_INFORMATION_FORMAT = dwarf; 209 | ENABLE_STRICT_OBJC_MSGSEND = YES; 210 | ENABLE_TESTABILITY = YES; 211 | GCC_C_LANGUAGE_STANDARD = gnu11; 212 | GCC_DYNAMIC_NO_PIC = NO; 213 | GCC_NO_COMMON_BLOCKS = YES; 214 | GCC_OPTIMIZATION_LEVEL = 0; 215 | GCC_PREPROCESSOR_DEFINITIONS = ( 216 | "DEBUG=1", 217 | "$(inherited)", 218 | ); 219 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 220 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 221 | GCC_WARN_UNDECLARED_SELECTOR = YES; 222 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 223 | GCC_WARN_UNUSED_FUNCTION = YES; 224 | GCC_WARN_UNUSED_VARIABLE = YES; 225 | IPHONEOS_DEPLOYMENT_TARGET = 13.1; 226 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 227 | MTL_FAST_MATH = YES; 228 | ONLY_ACTIVE_ARCH = YES; 229 | SDKROOT = iphoneos; 230 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 231 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 232 | }; 233 | name = Debug; 234 | }; 235 | C649F0532351F99500E23D98 /* Release */ = { 236 | isa = XCBuildConfiguration; 237 | buildSettings = { 238 | ALWAYS_SEARCH_USER_PATHS = NO; 239 | CLANG_ANALYZER_NONNULL = YES; 240 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 241 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 242 | CLANG_CXX_LIBRARY = "libc++"; 243 | CLANG_ENABLE_MODULES = YES; 244 | CLANG_ENABLE_OBJC_ARC = YES; 245 | CLANG_ENABLE_OBJC_WEAK = YES; 246 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 247 | CLANG_WARN_BOOL_CONVERSION = YES; 248 | CLANG_WARN_COMMA = YES; 249 | CLANG_WARN_CONSTANT_CONVERSION = YES; 250 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 251 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 252 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 253 | CLANG_WARN_EMPTY_BODY = YES; 254 | CLANG_WARN_ENUM_CONVERSION = YES; 255 | CLANG_WARN_INFINITE_RECURSION = YES; 256 | CLANG_WARN_INT_CONVERSION = YES; 257 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 258 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 259 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 260 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 261 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 262 | CLANG_WARN_STRICT_PROTOTYPES = YES; 263 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 264 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 265 | CLANG_WARN_UNREACHABLE_CODE = YES; 266 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 267 | COPY_PHASE_STRIP = NO; 268 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 269 | ENABLE_NS_ASSERTIONS = NO; 270 | ENABLE_STRICT_OBJC_MSGSEND = YES; 271 | GCC_C_LANGUAGE_STANDARD = gnu11; 272 | GCC_NO_COMMON_BLOCKS = YES; 273 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 274 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 275 | GCC_WARN_UNDECLARED_SELECTOR = YES; 276 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 277 | GCC_WARN_UNUSED_FUNCTION = YES; 278 | GCC_WARN_UNUSED_VARIABLE = YES; 279 | IPHONEOS_DEPLOYMENT_TARGET = 13.1; 280 | MTL_ENABLE_DEBUG_INFO = NO; 281 | MTL_FAST_MATH = YES; 282 | SDKROOT = iphoneos; 283 | SWIFT_COMPILATION_MODE = wholemodule; 284 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 285 | VALIDATE_PRODUCT = YES; 286 | }; 287 | name = Release; 288 | }; 289 | C649F0552351F99500E23D98 /* Debug */ = { 290 | isa = XCBuildConfiguration; 291 | buildSettings = { 292 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 293 | CODE_SIGN_STYLE = Automatic; 294 | DEVELOPMENT_ASSET_PATHS = "\"SearchBar/Preview Content\""; 295 | DEVELOPMENT_TEAM = 26F6YWJYL8; 296 | ENABLE_PREVIEWS = YES; 297 | INFOPLIST_FILE = SearchBar/Info.plist; 298 | LD_RUNPATH_SEARCH_PATHS = ( 299 | "$(inherited)", 300 | "@executable_path/Frameworks", 301 | ); 302 | PRODUCT_BUNDLE_IDENTIFIER = UPP.SearchBar; 303 | PRODUCT_NAME = "$(TARGET_NAME)"; 304 | SWIFT_VERSION = 5.0; 305 | TARGETED_DEVICE_FAMILY = "1,2"; 306 | }; 307 | name = Debug; 308 | }; 309 | C649F0562351F99500E23D98 /* Release */ = { 310 | isa = XCBuildConfiguration; 311 | buildSettings = { 312 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 313 | CODE_SIGN_STYLE = Automatic; 314 | DEVELOPMENT_ASSET_PATHS = "\"SearchBar/Preview Content\""; 315 | DEVELOPMENT_TEAM = 26F6YWJYL8; 316 | ENABLE_PREVIEWS = YES; 317 | INFOPLIST_FILE = SearchBar/Info.plist; 318 | LD_RUNPATH_SEARCH_PATHS = ( 319 | "$(inherited)", 320 | "@executable_path/Frameworks", 321 | ); 322 | PRODUCT_BUNDLE_IDENTIFIER = UPP.SearchBar; 323 | PRODUCT_NAME = "$(TARGET_NAME)"; 324 | SWIFT_VERSION = 5.0; 325 | TARGETED_DEVICE_FAMILY = "1,2"; 326 | }; 327 | name = Release; 328 | }; 329 | /* End XCBuildConfiguration section */ 330 | 331 | /* Begin XCConfigurationList section */ 332 | C649F03B2351F99000E23D98 /* Build configuration list for PBXProject "SearchBar" */ = { 333 | isa = XCConfigurationList; 334 | buildConfigurations = ( 335 | C649F0522351F99500E23D98 /* Debug */, 336 | C649F0532351F99500E23D98 /* Release */, 337 | ); 338 | defaultConfigurationIsVisible = 0; 339 | defaultConfigurationName = Release; 340 | }; 341 | C649F0542351F99500E23D98 /* Build configuration list for PBXNativeTarget "SearchBar" */ = { 342 | isa = XCConfigurationList; 343 | buildConfigurations = ( 344 | C649F0552351F99500E23D98 /* Debug */, 345 | C649F0562351F99500E23D98 /* Release */, 346 | ); 347 | defaultConfigurationIsVisible = 0; 348 | defaultConfigurationName = Release; 349 | }; 350 | /* End XCConfigurationList section */ 351 | }; 352 | rootObject = C649F0382351F99000E23D98 /* Project object */; 353 | } 354 | -------------------------------------------------------------------------------- /SearchBar.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SearchBar.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SearchBar.xcodeproj/xcuserdata/Uwe.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | SearchBar.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /SearchBar/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // SearchBar 4 | // 5 | // Created by Uwe Petersen on 12.10.19. 6 | // Copyright © 2019 Uwe Petersen. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | // MARK: UISceneSession Lifecycle 22 | 23 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 24 | // Called when a new scene session is being created. 25 | // Use this method to select a configuration to create the new scene with. 26 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 27 | } 28 | 29 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 30 | // Called when the user discards a scene session. 31 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 32 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 33 | } 34 | 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /SearchBar/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /SearchBar/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SearchBar/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 | -------------------------------------------------------------------------------- /SearchBar/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // SearchBar 4 | // 5 | // Created by Uwe Petersen on 12.10.19. 6 | // Copyright © 2019 Uwe Petersen. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | 11 | struct ContentView: View { 12 | let array = ["Peter", "Paul", "Mary", "Anna-Lena", "George", "John", "Greg", "Thomas", "Robert", "Bernie", "Mike", "Benno", "Hugo", "Miles", "Michael", "Mikel", "Tim", "Tom", "Lottie", "Lorrie", "Barbara"] 13 | 14 | @State private var searchText = "" 15 | 16 | var body: some View { 17 | 18 | NavigationView { 19 | VStack { 20 | 21 | // Search view 22 | SearchBarView(searchText: $searchText) 23 | 24 | List { 25 | // Filtered list of names 26 | ForEach(array.filter{$0.hasPrefix(searchText) || searchText == ""}, id:\.self) { 27 | searchText in Text(searchText) 28 | } 29 | } 30 | .navigationBarTitle(Text("Search")) 31 | .resignKeyboardOnDragGesture() 32 | } 33 | } 34 | } 35 | } 36 | 37 | 38 | 39 | struct ContentView_Previews: PreviewProvider { 40 | static var previews: some View { 41 | Group { 42 | ContentView() 43 | .environment(\.colorScheme, .light) 44 | 45 | ContentView() 46 | .environment(\.colorScheme, .dark) 47 | } 48 | } 49 | } 50 | 51 | extension UIApplication { 52 | func endEditing(_ force: Bool) { 53 | self.windows 54 | .filter{$0.isKeyWindow} 55 | .first? 56 | .endEditing(force) 57 | } 58 | } 59 | 60 | struct ResignKeyboardOnDragGesture: ViewModifier { 61 | var gesture = DragGesture().onChanged{_ in 62 | UIApplication.shared.endEditing(true) 63 | } 64 | func body(content: Content) -> some View { 65 | content.gesture(gesture) 66 | } 67 | } 68 | 69 | extension View { 70 | func resignKeyboardOnDragGesture() -> some View { 71 | modifier(ResignKeyboardOnDragGesture()) 72 | } 73 | } 74 | 75 | 76 | struct SearchBarView: View { 77 | 78 | @Binding var searchText: String 79 | @State private var showCancelButton: Bool = false 80 | var onCommit: () ->Void = {print("onCommit")} 81 | 82 | var body: some View { 83 | HStack { 84 | HStack { 85 | Image(systemName: "magnifyingglass") 86 | 87 | // Search text field 88 | ZStack (alignment: .leading) { 89 | if searchText.isEmpty { // Separate text for placeholder to give it the proper color 90 | Text("Search") 91 | } 92 | TextField("", text: $searchText, onEditingChanged: { isEditing in 93 | self.showCancelButton = true 94 | }, onCommit: onCommit).foregroundColor(.primary) 95 | } 96 | // Clear button 97 | Button(action: { 98 | self.searchText = "" 99 | }) { 100 | Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1) 101 | } 102 | } 103 | .padding(EdgeInsets(top: 8, leading: 6, bottom: 8, trailing: 6)) 104 | .foregroundColor(.secondary) // For magnifying glass and placeholder test 105 | .background(Color(.tertiarySystemFill)) 106 | .cornerRadius(10.0) 107 | 108 | if showCancelButton { 109 | // Cancel button 110 | Button("Cancel") { 111 | UIApplication.shared.endEditing(true) // this must be placed before the other commands here 112 | self.searchText = "" 113 | self.showCancelButton = false 114 | } 115 | .foregroundColor(Color(.systemBlue)) 116 | } 117 | } 118 | .padding(.horizontal) 119 | .navigationBarHidden(showCancelButton) 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /SearchBar/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UIApplicationSceneManifest 24 | 25 | UIApplicationSupportsMultipleScenes 26 | 27 | UISceneConfigurations 28 | 29 | UIWindowSceneSessionRoleApplication 30 | 31 | 32 | UISceneConfigurationName 33 | Default Configuration 34 | UISceneDelegateClassName 35 | $(PRODUCT_MODULE_NAME).SceneDelegate 36 | 37 | 38 | 39 | 40 | UILaunchStoryboardName 41 | LaunchScreen 42 | UIRequiredDeviceCapabilities 43 | 44 | armv7 45 | 46 | UISupportedInterfaceOrientations 47 | 48 | UIInterfaceOrientationPortrait 49 | UIInterfaceOrientationLandscapeLeft 50 | UIInterfaceOrientationLandscapeRight 51 | 52 | UISupportedInterfaceOrientations~ipad 53 | 54 | UIInterfaceOrientationPortrait 55 | UIInterfaceOrientationPortraitUpsideDown 56 | UIInterfaceOrientationLandscapeLeft 57 | UIInterfaceOrientationLandscapeRight 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /SearchBar/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SearchBar/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SceneDelegate.swift 3 | // SearchBar 4 | // 5 | // Created by Uwe Petersen on 12.10.19. 6 | // Copyright © 2019 Uwe Petersen. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SwiftUI 11 | 12 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 18 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 19 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 20 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 21 | 22 | // Create the SwiftUI view that provides the window contents. 23 | let contentView = ContentView() 24 | 25 | // Use a UIHostingController as window root view controller. 26 | if let windowScene = scene as? UIWindowScene { 27 | let window = UIWindow(windowScene: windowScene) 28 | window.rootViewController = UIHostingController(rootView: contentView) 29 | self.window = window 30 | window.makeKeyAndVisible() 31 | } 32 | } 33 | 34 | func sceneDidDisconnect(_ scene: UIScene) { 35 | // Called as the scene is being released by the system. 36 | // This occurs shortly after the scene enters the background, or when its session is discarded. 37 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 38 | // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). 39 | } 40 | 41 | func sceneDidBecomeActive(_ scene: UIScene) { 42 | // Called when the scene has moved from an inactive state to an active state. 43 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 44 | } 45 | 46 | func sceneWillResignActive(_ scene: UIScene) { 47 | // Called when the scene will move from an active state to an inactive state. 48 | // This may occur due to temporary interruptions (ex. an incoming phone call). 49 | } 50 | 51 | func sceneWillEnterForeground(_ scene: UIScene) { 52 | // Called as the scene transitions from the background to the foreground. 53 | // Use this method to undo the changes made on entering the background. 54 | } 55 | 56 | func sceneDidEnterBackground(_ scene: UIScene) { 57 | // Called as the scene transitions from the foreground to the background. 58 | // Use this method to save data, release shared resources, and store enough scene-specific state information 59 | // to restore the scene back to its current state. 60 | } 61 | 62 | 63 | } 64 | 65 | --------------------------------------------------------------------------------