How to stick my custom Tab to the bottom on every Iphone (with and without SafeArea)?

Viewed 36
struct EntryView : View {

@State private var isSelected = 0

var body : some View {

VStack {

if isSelected == 0 {

Text("home") }

}

Spacer()

TabBar(isSelected: $isSelected)

    }

}


struct TabBar: View {

@Binding isSelected : Int

var body : some View {

HStack {

Button(action: {

self.isSelected = 0 },
label: {
VStack {

if isSelected == 0 {

Image(systemName: "house.fill")
} else {

Image(systemName: "house")
}

}

}

}.background(LinearGradient())
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 12)

}

}

I want to stick my custom Tab to the bottom of the screen on the every sized screen. On the iphone 8 it is on the bottom but on iphone 12 on example it is not. I need to bypass safe area on iphones with notches but to stay on iphone 8 etc.

1 Answers

You can check whether there is a notch or not like this


extension UIDevice {
    var hasNotch: Bool {
        if #available(iOS 11.0, *) {
            let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
            return keyWindow?.safeAreaInsets.bottom ?? 0 > 0
        }
        return false
    }
}

if UIDevice.current.hasNotch {
    VStack{

    }
    .ignoresSafeArea()
} else {
    VStack{

    }
}
Related