在SwiftUI中隐藏导航控制器底部线

4
如何在SwiftUI中隐藏UINavigationController的底部栏?到目前为止,我只找到了针对UIKit的解决方案,但没有找到SwiftUI的解决方案。

enter image description here

3个回答

2
请看被采纳的答案:SwiftUI Remove NavigationBar Bottom Border 改变前:enter image description here 改变后: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()
    }
}


4
这对我没有起作用。 - tHatpart
1
有示例代码吗?这对我非常有效。我已经加入了整个带图像的代码。 - Vijay Lama
仍然无法工作。我认为问题在于 SwiftUI 视图通过 UIHostingController 推入了 UIKit UINavigationController 中,我们能在那里做些什么吗? - tHatpart

1
这是一个在SwiftUI中隐藏导航栏底部分隔线的完整工作代码。
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

0

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接