Home > AI > IOS > SwiftUI >

get default navigationBar height

The idea is as same as getting default padding value. We need to create @Binding value.

NavigationConfigration.swift
(access the attribute of navigationController of UIViewController)

struct NavigationConfiguration: UIViewControllerRepresentable {
    var config: (UINavigationController) -> Void = { _ in }
    func makeUIViewController(context: Context) -> some UIViewController {
        let controller = UIViewController()
        DispatchQueue.main.async {
            if let nc = controller.navigationController {
                self.config(nc)
            }
        }
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        if let nc = uiViewController.navigationController {
            self.config(nc)
        }
    }
}

GetDefaultNavBarHeightView.swift (get the defaultNavHeight)

import SwiftUI

struct GetDefaultNavBarHeightView: View {
    @Binding var defaultNavHeight: CGFloat
    @State var isHidden: Bool = false
    
    
    var body: some View {
        if !isHidden {
            Text("")
                .background(
                        NavigationConfiguration(config: { (nc) in
                        self.defaultNavHeight = nc.navigationBar.frame.height
                        })
                )
        }
    }
}

ContentView.swift (pass the defaultNavHeight)

struct ContentView: View {
 
    @State var defaultNavHeight: CGFloat = 0
    
 
    var body: some View {
        NavigationView {
            ZStack {
                GetDefaultNavBarHeightView(defaultNavHeight: $defaultNavHeight)
                
                Text("good")
            }
            .navigationBarTitleDisplayMode(.inline)
            .navigationBarItems(trailing: NavRightView(defaultNavHeight: defaultNavHeight))
        }
    }
}

NavRightView.swift (use the defaultNavHeight)

import SwiftUI

struct NavRightView: View {
    var defaultNavHeight: CGFloat
    
    init(defaultNavHeight: CGFloat) {
        self.defaultNavHeight = defaultNavHeight
    }
    
    var body: some View {
        HStack {
            NavRightUnitView(imgName: "material", des: "资料圈", defaultNavHeight: self.defaultNavHeight)
            Spacer()
            NavRightUnitView(imgName: "group", des: "备考群", defaultNavHeight: self.defaultNavHeight)
        }
    }
}




struct NavRightUnitView: View {
    var defaultNavHeight: CGFloat
    var imgName: String
    var des: String
    
    init(imgName: String, des: String, defaultNavHeight: CGFloat) {
        self.imgName = imgName
        self.des = des
        self.defaultNavHeight = defaultNavHeight
    }
    
    var body: some View {
        VStack {
            Image(imgName)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 0.55*self.defaultNavHeight, height: 0.55*self.defaultNavHeight, alignment: .center)
        
            
            Text(des)
                .font(.system(size: 0.25*self.defaultNavHeight))
        }
    }
}

Leave a Reply