@Published property wrapper not working on subclass of ObservableObject

Finally figured out a solution/workaround to this issue. If you remove the property wrapper from the subclass, and call the baseclass objectWillChange.send() on the variable the state is updated properly. NOTE: Do not redeclare let objectWillChange = PassthroughSubject<Void, Never>() on the subclass as that will again cause the state not to update properly. I hope … Read more

SwiftUI withAnimation completion callback

Unfortunately there’s no good solution to this problem (yet). However, if you can specify the duration of an Animation, you can use DispatchQueue.main.asyncAfter to trigger an action exactly when the animation finishes: withAnimation(.linear(duration: 0.1)) { self.someState = newState } DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { print(“Animation finished”) }

Programmatically detect Tab Bar or TabView height in SwiftUI

As bridge to UIKit is officially allowed and documented, it is possible to read needed information from there when needed. Here is possible approach to read tab bar height directly from UITabBar // Helper bridge to UIViewController to access enclosing UITabBarController // and thus its UITabBar struct TabBarAccessor: UIViewControllerRepresentable { var callback: (UITabBar) -> Void … Read more

How to change the colors of a segment in a UISegmentedControl in iOS 13?

As of iOS 13b3, there is now a selectedSegmentTintColor on UISegmentedControl. To change the overall color of the segmented control use its backgroundColor. To change the color of the selected segment use selectedSegmentTintColor. To change the color/font of the unselected segment titles, use setTitleTextAttributes with a state of .normal/UIControlStateNormal. To change the color/font of the … Read more

iOS 13.1 Crash in AVAudio Player

I found a crash issue in AVAudioPlayer with iOS 13.1. Here Is Solution Why My AVAudioPlayer crash? because I initialise AVAudioPlayer like var wrongMusicPlayer: AVAudioPlayer = AVAudioPlayer() and then i try to reassign wrongMusicPlayer as below wrongMusicPlayer = try AVAudioPlayer(contentsOf: wrongURL) And my app get crash. Solution If you initialise your AVAudioPlayer like var wrongMusicPlayer: … Read more

SwiftUI @State and .sheet() ios13 vs ios14

Your code have expectation of view update/creation order, but in general it is undefined (and probably changed in iOS 14). There is explicit way to pass information inside sheet – use different sheet creator, ie. .sheet(item:… Here is working reliable example. Tested with Xcode 12 / iOS 14 struct ContentView: View { @State private var … Read more

SwiftUI @Binding Initialize

When you use your LoggedInView in your app you do need to provide some binding, such as an @State from a previous view or an @EnvironmentObject. For the special case of the PreviewProvider where you just need a fixed value you can use .constant(false) E.g. #if DEBUG struct LoggedInView_Previews : PreviewProvider { static var previews: … Read more