Keyboard Observations and SwiftUI Animations
Maps UIKit SwiftUI
Today's episode will focus on a couple of bug fixes regarding the UI of our application. First I'll discuss the strange issue with the focus of our search TextField. Secondly we'll go over how to observe the keyboard in our application and make sure that the carousel remains on top of the keyboard.

Comments (1)
frankusu
3 years ago
Hey Brian, superb content as always. You always add so much value to existing lessons!! I came from episode 7 and 16, I incorporated your code in Swift, not SwiftUI. Same behaviour. Replace for Swift projects in the observers UIView.animate(withDuration: 0.3, animations: { self?.locationsController.view!.frame.origin.y -= (self!.keyboardHeight - self!.view.safeAreaInsets.bottom + 5) //+5 is so the carousel doesn't stick exactly to keyboard's top }, completion: nil) UIView.animate(withDuration: 0.3) { self?.locationsController.view!.frame.origin.y += self!.keyboardHeight } Question1: Is there a way to dynamically set the carousel constraint like the custom image callout in episode 21 instead of using "locationsController.view!.frame.origin.y"? Episode 21 Code if image.size.width > image.size.height { // w1/h1 = w2/h2; h1 = w1/w2*h2 let newWidth : CGFloat = 180 let newHeight = newWidth / image.size.width * image.size.height widthAnchor.constant = newWidth heightAnchor.constant = newHeight Question2: This is probably a tricky question. When I first search with no carousel, I get the following warning. How do I make it so the first search has the carousel on top of the keyboard? [UICollectionView] Warning: Invalid IndexPath <NSIndexPath: 0xdee85593a171916d> {length = 2, path = 0 - 0} specified - will use a contentOffset of {0,0} as a fallback value. <UICollectionView: 0x103832c00; frame = (0 0; 375 100); autoresize = W+H; gestureRecognizers = <NSArray: 0x28376a4c0>; layer = <CALayer: 0x2839c3da0>; contentOffset: {0, 0}; contentSize: {32, 100}; adjustedContentInset: {0, 0, 0, 0}; layout: <UICollectionViewFlowLayout: 0x10b74d800>; dataSource: <MapKitTeachMe.LocationsCarouselController: 0x10b74da20>> Question3: for "self?.locationsController.view!.frame.origin.y += self!.keyboardHeight" is "self!" safe? and what does "self!" really mean? Thank you so much Brian!!
HELP & SUPPORT