首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TabView动画故障

TabView动画故障
EN

Stack Overflow用户
提问于 2022-05-19 22:25:15
回答 1查看 51关注 0票数 0

找出那个奇怪的小故障,却无法解决。问题的来源- TabView和改变大小的子视图与动画。更改UITabbar外观没有帮助。改变安全区的选择没有帮助。

UITabBar.appearance().isHidden = false,不透明的外观显示整个选项卡的闪烁。我想隐藏默认的UITabbar来定制我自己的。有什么想法吗?

底部安全区域的闪烁:演示GIF

样本项目:

代码语言:javascript
复制
struct ContentView: View {

    @State var selected = "second"

    var body: some View {
        ZStack(alignment: .bottom) {
            VStack(spacing: 0) {
                FirstView()
                TabView(selection: $selected) {
                    SecondView()
                        .tabItem({
                            Text("second")
                        })
                        .tag("second")
                    ThirdView()
                        .tabItem({
                            Text("third")
                        })
                        .tag("third")
                }
            }
            HStack {
                Image(systemName: selected == "second" ? "circle.fill" : "circle")
                    .onTapGesture {
                        selected = "second"
                    }
                Image(systemName: selected == "third" ? "circle.fill" : "circle")
                    .onTapGesture {
                        selected = "third"
                    }
            }
            .frame(width: 70, height: 40, alignment: .center)
            .background(Color.white)
            .cornerRadius(10)
            .padding()
        }
        .edgesIgnoringSafeArea(.all)
        .onAppear {
            UITabBar.appearance().isHidden = true
        }
    }
}
代码语言:javascript
复制
struct FirstView: View {
    
    @State var height:CGFloat = 200
    
    var body: some View {
        ZStack(alignment: .bottom) {
            Color.red
                .frame(height: height)
            Image(systemName: height == 200 ? "arrow.down" : "arrow.up")
                .foregroundColor(.white)
                .padding(5)
                .onTapGesture {
                    withAnimation(.easeInOut(duration: 2)) {
                        height = height == 200 ? 350 : 200
                    }
                }
        }
    }
}
代码语言:javascript
复制
struct SecondView: View {
    var body: some View {
        ZStack {
            Text("hello")
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
        .background(Color.green)
        .onAppear {
            UITabBar.appearance().isHidden = true
        }
    }
}
代码语言:javascript
复制
struct ThirdView: View {
    var body: some View {
        Color.blue
            .edgesIgnoringSafeArea(.bottom)
    }
}

谢谢你的回答:)

EN

回答 1

Stack Overflow用户

发布于 2022-05-20 05:48:25

由于您有自己的索引导航,所以我建议完全摆脱TabView,通过if / elseswitch语句切换视图:

代码语言:javascript
复制
struct ContentView: View {

    @State var selected = "second"

    var body: some View {
        ZStack(alignment: .bottom) {
            VStack(spacing: 0) {
                FirstView()
                if selected == "second" {
                    SecondView()
                } else {
                    ThirdView()
                }
            }
            HStack {
                Image(systemName: selected == "second" ? "circle.fill" : "circle")
                    .onTapGesture {
                        selected = "second"
                    }
                Image(systemName: selected == "third" ? "circle.fill" : "circle")
                    .onTapGesture {
                        selected = "third"
                    }
            }
            .frame(width: 70, height: 40, alignment: .center)
            .background(Color.white)
            .cornerRadius(10)
            .padding()
        }
        .edgesIgnoringSafeArea(.all)
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72311696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档