隐藏SwiftUI中导航控制器上的底线

2vuwiymt  于 2023-02-07  发布在  Swift
关注(0)|答案(2)|浏览(179)

如何在带有SwiftUI的UINavigationController上隐藏这个底部栏?到目前为止,我只找到了UIKit的解决方案,但没有找到SwiftUI的解决方案。

ruarlubt

ruarlubt1#

看看被接受的答案:SwiftUI Remove NavigationBar Bottom Border
之前:

之后:

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()
    }
}
kgqe7b3p

kgqe7b3p2#

我在使用UIHostingController时也遇到了同样的问题,所以我最终向UIViewController添加了一个子UIHostingController,并以这种方式设置了阴影。

@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)
}

相关问题