4

How do I hide this bottom bar on a UINavigationController with SwiftUI? So far I have found only solutions for UIKit, but nothing for SwiftUI.

enter image description here

tHatpart
  • 1,302
  • 3
  • 12
  • 27

3 Answers3

1

Look at the accepted answer: SwiftUI Remove NavigationBar Bottom Border

Before: enter image description here After: enter image description here


import SwiftUI
struct TestView: View {
    init(){
        let appearance = UINavigationBarAppearance()
            appearance.shadowColor = .clear
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
    }
    var body: some View {
        NavigationView{
            ScrollView{
                ForEach(0 ..< 20){ num in
                        Text("Num - \(num)")
                        .padding()
                }
            }
            .navigationTitle("Learn")
        }
    }
}

struct TestView_Previews: PreviewProvider {
    static var previews: some View {
        TestView()
    }
}

Vijay Lama
  • 96
  • 1
  • 6
0

I had the same issue when using a UIHostingController. So I ended up adding a child UIHostingController to a UIViewController and setting the shadow that way.

@IBOutlet weak var theContainer: UIView!

override func viewDidLoad() {
 super.viewDidLoad()

  let appearance = UINavigationBarAppearance()
  appearance.backgroundColor = UIColor(Color("navbackground"))
  appearance.shadowColor = .clear
  self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
  self.navigationController?.navigationBar.standardAppearance = appearance

  let childView = UIHostingController(rootView: SettingsView())
  addChild(childView)

  childView.view.frame = theContainer.bounds
  theContainer.addSubview(childView.view)
  childView.didMove(toParent: self)
}
Edward Tattsyrup
  • 245
  • 1
  • 3
  • 15
0

This is a complete working code in SwiftUI to hide bottom seprator line in navigation bar:

let coloredAppearance = UINavigationBarAppearance() 
coloredAppearance.configureWithOpaqueBackground() 
coloredAppearance.backgroundColor = UIColor(Color.green) 
coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor(.white)] 
coloredAppearance.largeTitleTextAttributes = [ .foregroundColor: UIColor(.white), .font: UIFont.systemFont(ofSize: 18) ] 

coloredAppearance.shadowColor = .clear
coloredAppearance.shadowImage = UIImage() 

UINavigationBar.appearance().standardAppearance = coloredAppearance 
UINavigationBar.appearance().compactAppearance = coloredAppearance
UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
oneshot
  • 591
  • 1
  • 5
  • 27