Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

How to solve this top bar problem on SwiftUI, elegantly?

I am trying to use the whole iPhone area for my app.

I have this HStack at the top, used to create a custom toolbar.

var body: some View {

  VStack (spacing:0) {
  
    MyTopbar()
    // other controls

    Spacer()
  }
  .edgesIgnoringSafeArea(.top)

This appears like this on new devices with a notch and old devices without a notch. The notch cuts my menu.

enter image description here

I can solve that by adding a spacer with a frame height before MyTopbar() on the vertical stack but first of all this seems to be a very awful solution. First I have to guess a height for that spacer. Then I have to detect if the device has a notch or not (?).

Is there a better way?

question from:https://stackoverflow.com/questions/65884073/how-to-solve-this-top-bar-problem-on-swiftui-elegantly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can think of it as layers (content that respects safe area and content that doesn't).

Something like this perhaps:

struct ContentView: View {
    var body: some View {
        ZStack {
            Color.blue.ignoresSafeArea() // Whatever view fills the whole screen

            VStack (spacing:0) {
                MyTopbar()
                // other controls
                Spacer()
            }
        }
    }
}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...