You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI修复头像按钮跳转至ProfileView问题求助

解决SwiftUI中点击头像按钮后导航栏消失的问题

你遇到的问题核心是错误地用showProfile状态来偏移隐藏ProfileIconView:当点击头像触发showProfile.toggle()后,导航栏被设置了offset(y: 900),直接移出屏幕可视范围,导致看起来消失了。同时当前代码也未实现跳转到ProfileView的逻辑。

修复方案

  1. 移除ProfileIconView上错误的偏移修饰符,避免导航栏被隐藏
  2. 添加页面跳转逻辑(推荐用NavigationStack或sheet实现)
  3. 优化导航栏布局适配不同屏幕

以下是修复后的完整代码:

import SwiftUI

struct NewHomeView: View {
    @State var show = false
    @State var showProfile = false
    @State var viewState = CGSize.zero
    @State private var searchText = ""
    @State var search = false
    @Binding var showIcon: Bool
     
    var body: some View {
        NavigationStack {
            ZStack {
                // 移除错误的offset修饰符,保留模糊和透明度控制
                ProfileIconView(showProfile: $showProfile)
                    .blur(radius: show ? 20 : 0)
                    .opacity(search ? 0 : 1)
                    .navigationBarHidden(true) // 隐藏系统导航栏,使用自定义导航栏
                
                Text("Your sets")
                    .offset(y: -100)
                    .fontWeight(.bold)
                    .font(.system(size: 30))
                
                NewSetView()
                    .offset(x: viewState.width, y: viewState.height )
                    .animation(.spring(response: 0.3, dampingFraction: 0.6, blendDuration: 0), value: viewState)
                    .onTapGesture {
                        self.show.toggle()
                    }
                    .gesture(
                        DragGesture().onChanged { value in
                            self.viewState = value.translation
                            self.show = true
                        }
                        .onEnded { value in
                            self.viewState = .zero
                            self.show = false
                        }
                    )
            }
            // 导航到个人中心页面
            .navigationDestination(isPresented: $showProfile) {
                ProfileView()
            }
        }
    }
}

// 个人中心页面示例
struct ProfileView: View {
    var body: some View {
        ZStack {
            Color.white
            Text("个人中心页面")
                .font(.largeTitle)
                .fontWeight(.bold)
        }
        .ignoresSafeArea()
    }
}

struct NewSetView: View {
    var body: some View {
        ZStack {
            HStack {
                VStack {
                    Image(systemName: "plus")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 50, height: 30)
                }
                .frame(width: 250, height: 180)
                .background(.gray)
                .cornerRadius(20)
                .shadow(radius: 20)
                .offset(y: 50)
            }
        }
    }
}

struct ProfileIconView: View {
    @Binding var showProfile: Bool
    var body: some View {
        VStack {
            HStack {
                Text("Flashy")
                    .font(.system(.title))
                    .fontWeight(.bold)
                
                Spacer()
                
                Button(action: {
                    showProfile.toggle()
                }) {
                    Image(systemName: "person.crop.circle")
                        .resizable()
                        .frame(width: 30, height: 30)
                }
            }
            .frame(width: UIScreen.main.bounds.width - 40) // 适配屏幕宽度,避免超出边界
            .padding(.top, 40) // 适配顶部安全区
            
            Spacer()
        }
    }
}

struct NewHomeView_Previews: PreviewProvider {
    static var previews: some View {
        NewHomeView(showIcon: .constant(false))
    }
}

额外说明

  • 若需兼容iOS15及以下,可将NavigationStack替换为NavigationView,并使用.sheet(isPresented: $showProfile) { ProfileView() }实现弹窗式跳转
  • 也可根据需求调整导航栏的样式、位置,或添加更多交互逻辑

内容的提问来源于stack exchange,提问作者syphixy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 11:43:17