[SwiftUI]: TabBar 后面没有透明度

huangapple go评论66阅读模式
英文:

[SwiftUI]: Not transparency behind TabBar

问题

我刚刚开始学习SwiftUI,我遇到了一个问题:为什么我的TabBar在滚动时不是透明的?

这是我的代码:

var body: some View {
    VStack {
        ZStack(alignment: .bottomTrailing) {
            ScrollViewReader { scroll in
                // 在这里添加我的气泡
            }
            .padding(.bottom, 70)
            .scrollDismissesKeyboard(.interactively)

            ZStack {
                HStack {
                    TextField("输入你的消息", text: $newMessage)
                        .padding(9)
                        .background(
                            RoundedRectangle(cornerRadius: 19)
                                .foregroundColor(Color(.systemGray6))
                        )
                }
                .padding()
            }
        }
    }
}

谢谢,祝你有一个美好的一天!

英文:

I just started to learn SwiftUI, and I have an issue: why my TabBar isn't transparent when scrolling ?

[SwiftUI]: TabBar 后面没有透明度

Here's my code :

var body: some View {
    VStack {
        ZStack(alignment: .bottomTrailing) {
            ScrollViewReader { scroll in
                // my bubbles here
            }
            .padding(.bottom, 70)
            .scrollDismissesKeyboard(.interactively)
        
            ZStack {
                HStack {
                    TextField("Type your message", text: $newMessage)
                        .padding(9)
                        .background(
                            RoundedRectangle(cornerRadius: 19)
                                .foregroundColor(Color(.systemGray6))
                        )
                }
                .padding()
            }
        }
    }
}

Thank you and have a great day !

答案1

得分: 2

要实现半透明的选项卡栏,您需要将文本字段和发送按钮放在ToolbarItem中,如下所示:

var body: some View {
    ScrollView {
        ForEach(0..<100) { _ in
            Capsule().fill(Color.blue)
                .frame(height: 50)
                .frame(maxWidth: .infinity)
                .padding(.horizontal)
        }
    }
    .toolbar {
        ToolbarItem(placement: .bottomBar) {
            HStack {
                TextField("输入您的消息", text: .constant(""))
                    .textFieldStyle(.roundedBorder)
                Button("发送", action: {})
            }
        }
    }
}

这将在工具栏和选项卡栏上都给您模糊的背景效果。

英文:

To have a semi-transparent tab bar you will have to place your text field and send button in a ToolbarItem like so:

var body: some View {
    ScrollView {
        ForEach(0..&lt;100) {_ in
            Capsule().fill(Color.blue)
                .frame(height: 50)
                .frame(maxWidth: .infinity)
                .padding(.horizontal)
        }
    }
    .toolbar {
        ToolbarItem(placement: .bottomBar) {
            HStack {
                TextField(&quot;Type your message&quot;, text: .constant(&quot;&quot;))
                    .textFieldStyle(.roundedBorder)
                Button(&quot;Send&quot;, action: {})
            }
        }
    }
}

Which gives you the blurry background both on tool bar and tab bar:

[SwiftUI]: TabBar 后面没有透明度

huangapple
  • 本文由 发表于 2023年5月28日 17:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定