0

I'm not sure if this is a bug with the iOS 16 betas (currently running iOS Developer Beta 3 on Xcode 14 beta 3) or if I'm doing something wrong (most likely the latter).

I'm trying to add a SwiftUI view to my existing UIKit app which uses UINavigationController. When I push the SwiftUI view (using UIHostingController), the destination (SwiftUI) view has this weird animation bug where the content loads for a split second, then it will jump up to the correct position. I'm setting the title for the NavigationBar to be .inline or blank completely and it seems like the title is initially being rendered in the .large format then, once it changes it to .inline, the content moves up to fill in the space once taken by the Title.

I've created a sample app to show this and submitted the bug report to Apple, but on the good chance I'm screwing something up, I'm hoping someone has any idea since I'm not likely to get a response from Apple.

Here's the sample project: https://github.com/rjeitani/testNavBarIssue

Relevant code:

// The UIKit ViewController

func showSwiftUIView() {
        let vc = UIHostingController(rootView: SwiftUIView())
        self.navigationController?.pushViewController(vc, animated: true)
        
    }

// SwiftUI View

import SwiftUI
import Charts

struct SwiftUIView: View {
    var body: some View {
        testView()
    }
}


struct testView: View {
    
    @State private var pickerState: Bool = false
    
    var body: some View {
        VStack (alignment: .leading) {
            Text("Test").padding()
            Chart {
                BarMark(x: .value("Month", 1), y: .value("Count", 3))
                BarMark(x: .value("Month", 2), y: .value("Count", 7))
                BarMark(x: .value("Month", 3), y: .value("Count", 2))
                BarMark(x: .value("Month", 4), y: .value("Count", 1))
            }
        } .navigationTitle("")
            .navigationBarTitleDisplayMode(.inline)
    }
    
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

Here's a screen recording of the bug. You can see near the end that the "Test" label and the whole Chart itself jump upwards after the view has loaded.

Screen Recording showing animation bug

Rachid
  • 315
  • 3
  • 11
  • .inline appears to cause the problem(bug). Since title is blank, why not just remove .navigationTitle("") and .navigationBarTitleDisplayMode(.inline) to use the default(.automatic)? – Marcy Jul 13 '22 at 03:10
  • 1
    Unfortunately, that doesn't solve it. Instead of that weird animation jump, the view just stays at that initial position and leaves a blank space up top. – Rachid Jul 13 '22 at 16:17

1 Answers1

1

Faced the same issue, fixed it by avoiding SwiftUI's navigation viewModifiers (.navigationTitle() and .navigationBarTitle()) altogether and instead using UIKit's functions after UIHostingController is initialized.

In your case that would be inside the showSwiftUI() func like so:

func showSwiftUIView() {
    let vc = UIHostingController(rootView: SwiftUIView())
    // This sets the NavigationBar title
    vc.title = ""
    // This sets NavigationBar title to be "inline"
    vc.navigationItem.largeTitleDisplayMode = .never
    self.navigationController?.pushViewController(vc, animated: true)        
}

Alternatively I highly recommend you use Coordinator pattern along with UIKit-SwiftUI navigation as it'll help abstract away the navigation logic along with navigation-specific view modifications in a separate file.

dramikei
  • 11
  • 2