0

您现在可以使用.toolbar-modifier 随意设置 NavigationBar 主体内容:

.toolbar {
    ToolbarItem(placement: .principal) {
        Text("Custom Title")
            .font(.title)
    }
}

是否有一种同样简单的方法来自定义后退按钮(文本和/或整个按钮),不涉及隐藏默认按钮并创建自己的按钮,然后还需要重新创建正确的功能(即实际返回并启用滑动回去)?

4

2 回答 2

0

同样简单,没有没有办法做到这一点。SwiftUI 用于基本编码,它是一个入门包。

但是,如果您深入UIKit了解魔术真正发生的地方,那么没有比选择您想要修改的内容并告诉它您想要的内容更简单的了。

下面的代码是一般的导航栏,背景,标题,后退按钮图像,后退按钮标题等。它会影响你的整个应用程序。它不完整,有一些速成,但你应该对如何制作它有一个不错的了解。

struct HomeView: View{
    @State var ispresented = true
    var body: some View {
        ZStack {
            NavigationView {
                NavigationLink(
                    destination: ListView(),
                    isActive: $ispresented,
                    label: {
                        Text("List View")
                    }).navigationTitle("Home")
            }
        }
    }
}
struct ListView: View{
    init() {
    }
    var body: some View {
        ZStack {
            List{
                Text("List")
            }.navigationTitle("Sample")
            
        }
    }
}
struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}
extension UINavigationController {
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        //.inline
        let standard = navigationBar.standardAppearance
        standard.backgroundColor = .blue
        standard.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.yellow,NSAttributedString.Key.font: UIFont(name: UIFont.familyNames[4], size: 20)!]
        //This one is for standard and compact
        standard.setBackIndicatorImage(UIImage(systemName: "checkmark"), transitionMaskImage: UIImage(systemName: "checkmark"))
        
        //Landscape
        let compact = navigationBar.compactAppearance
        compact?.backgroundColor = .blue
        compact?.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.red]
        
        //.large
        let scrollEdge = navigationBar.standardAppearance
        //This image overrides standard and compact
        scrollEdge.setBackIndicatorImage(UIImage(systemName: "plus"), transitionMaskImage: UIImage(systemName: "plus"))
        
        scrollEdge.backgroundColor = .blue
        
        scrollEdge.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.green,NSAttributedString.Key.font: UIFont(name: UIFont.familyNames[2], size: 48)!]
        
        scrollEdge.backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.magenta]
        
        navigationBar.standardAppearance = standard
        navigationBar.compactAppearance = compact
        navigationBar.scrollEdgeAppearance = scrollEdge
        
        //This color the Back Button Image
        navigationBar.tintColor = .brown
        
    }
}
于 2021-01-23T17:11:19.617 回答
0

使用UIButton().

struct ToolbarButton: UIViewRepresentable {

    private let sfSymbolName: String
    
    private let titleColor: UIColor
    
    private let action: () -> ()
    
    internal init(sfSymbolName: String, titleColor: UIColor, action: @escaping () -> ()) {
        self.sfSymbolName = sfSymbolName
        self.titleColor = titleColor
        self.action = action
    }

    
    func makeUIView(context: Context) -> UIButton {
        let button = UIButton()
        let largeConfig = UIImage.SymbolConfiguration(scale: .large)


        // Use custom icon instead of system icons.

        let image = UIImage(systemName: sfSymbolName, withConfiguration: largeConfig)
        button.setImage(image, for: .normal)
        button.tintColor = titleColor
        button.contentEdgeInsets = UIEdgeInsets(top: 4, left: 4, bottom: 4, right: 4)
        button.addTarget(context.coordinator, action: #selector(context.coordinator.didTapButton(_:)), for: .touchUpInside)
        return button
    }
    
    func updateUIView(_ uiView: UIButton, context: Context) {}
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(action: action)
    }
    
    
    class Coordinator {
        private let action: () -> ()
        
        init(action: @escaping () -> ()) {
            self.action = action
        }
        
        @objc
        func didTapButton(_ sender: UIButton) {
            self.action()
        }
    }
}

struct CloseButton: View {
    var onClose: () -> ()
    var spacing: CGFloat

    init(spacing: CGFloat = 2, onClose: @escaping () -> ()) {
        self.spacing = spacing
        self.onClose = onClose
    }
    
    var body: some View {
        ToolbarButton(sfSymbolName: "plus", titleColor: UIColor(Color.accentColor), action: self.onClose)
            .rotationEffect(.degrees(45), anchor: .center)
            .padding(2)
            .background(Circle().fill(Color.systemGray2))
            .padding(2)
            .animation(.easeOut)
    }
}
于 2021-01-23T15:01:42.740 回答