ios - 如何在 SwiftUI 中的大型导航栏标题旁边显示配置文件图标?

标签 ios swift image swiftui profile

我正在开发一个支持多个配置文件的应用程序。我真的很喜欢 Apple 显示 Profile Icon 的方式在 Large Navigation Bar Title 旁边在他们所有的应用程序中。请参阅下面的屏幕截图:
Screenshot Example
我的问题如下:

  • 是否有可能在 SwiftUI 中实现这一点? ?如果是这样,怎么办?
  • 如果在纯 SwiftUI 中不可能,我怎样才能实现它,包括UIKit代码?

  • 谢谢你的帮助。

    最佳答案

    我通过使用 SwiftUI-Introspect 解决了这个问题, 到“从 SwiftUI 内省(introspection)底层 UIKit 组件”。
    下面是一个 View 示例:

    struct ContentView: View {
    
        @State private var lastHostingView: UIView!
        
        var body: some View {
            NavigationView {
                ScrollView {
                    ForEach(1 ... 50, id: \.self) { index in
                        Text("Index: \(index)")
                    }
                    .frame(maxWidth: .infinity)
                }
                .navigationTitle("Large title")
                .introspectNavigationController { navController in
                    let bar = navController.navigationBar
                    let hosting = UIHostingController(rootView: BarContent())
                    
                    guard let hostingView = hosting.view else { return }
                    // bar.addSubview(hostingView)                                          // <--- OPTION 1
                    // bar.subviews.first(where: \.clipsToBounds)?.addSubview(hostingView)  // <--- OPTION 2
                    hostingView.backgroundColor = .clear
                    
                    lastHostingView?.removeFromSuperview()
                    lastHostingView = hostingView
                    
                    hostingView.translatesAutoresizingMaskIntoConstraints = false
                    NSLayoutConstraint.activate([
                        hostingView.trailingAnchor.constraint(equalTo: bar.trailingAnchor),
                        hostingView.bottomAnchor.constraint(equalTo: bar.bottomAnchor, constant: -8)
                    ])
                }
            }
        }
    }
    
    酒吧内容和个人资料图片 View :
    struct BarContent: View {
        var body: some View {
            Button {
                print("Profile tapped")
            } label: {
                ProfilePicture()
            }
        }
    }
    
    struct ProfilePicture: View {
        var body: some View {
            Circle()
                .fill(
                    LinearGradient(
                        gradient: Gradient(colors: [.red, .blue]),
                        startPoint: .topLeading,
                        endPoint: .bottomTrailing
                    )
                )
                .frame(width: 40, height: 40)
                .padding(.horizontal)
        }
    }
    
    .frame(width: 40, height: 40) & hostingView.bottomAnchor constant将需要根据您的需要进行调整。
    每个选项的结果(在代码中注释):


    选项1
    选项 2


    滚动时查看棒
    滚动时 View 在下方消失

    Result - Option 1
    Result - Option 2

    关于ios - 如何在 SwiftUI 中的大型导航栏标题旁边显示配置文件图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67202400/

    相关文章:

    ios - 使用响应式编程将具有 imageURL 的对象转换为具有下载的 UIImage 的对象

    ios - 当 UITextField 在执行自定义 segue 后成为第一响应者时,键盘出现两次

    excel - 如何更改图像的定位 : "Move and size with cells" in Excel with openpyxl

    ios - Storyboard似乎未正确加载

    ios - "Expression was too complex to be solved in reasonable time..."是什么意思,我该如何解决?

    ios - Swift/iOS - 推送新 View Controller 时保留 UINavigationItem

    ios - 无法使用类型为 '[NSObject : AnyObject]' 的索引下标类型为 'NSKeyValueChangeKey' 的值

    ios - 自定义应用程序默认 NSDictionary

    html - 使分区图像响应

    css - :active background image?