0

I'm trying to show red color in full screen.

If I use edgesIgnoringSafeArea(.all) the screen automatically becomes scrolling enabled, which I don't want. Can you please advise me how to show red color on full screen without scrolling and without stretching because i change the color to image.

Any help would be greatly appreciated.


Sample code is given below.

import SwiftUI

struct PageSetup: View {

  @State private var tabSelection = 0

  var body: some View {
    ZStack {
      TabView(selection: $tabSelection) {
        ForEach(0..<5) { index in
          ZStack {
            Color.red
            Text("\(index)")
          }
        }
      }
      .tabViewStyle(PageTabViewStyle())
      .onAppear {
        UIScrollView.appearance().bounces = false
      }
      .tabViewStyle(PageTabViewStyle())
    }
  }
}

Output

egeeke
  • 753
  • 1
  • 6
  • 18
Sham Dhiman
  • 1,348
  • 1
  • 21
  • 59

1 Answers1

1

If I understood what you wanted correctly, you are using ZStack and Color in wrong place. Your body should be like this code sample.


struct PageSetup: View {

  @State private var tabSelection = 0

  var body: some View {
    ZStack {
      getColorForPage().ignoresSafeArea()
      TabView (selection: $tabSelection) {
        ForEach(0..<5){ index in
          Text("\(index)")
        }
      }
      .tabViewStyle(PageTabViewStyle())
      .onAppear {
        UIScrollView.appearance().bounces = false
      }
    }
  }

  func getColorForPage() -> Color {
    if tabSelection == 0 {
      return Color.red
    } else if tabSelection == 1 {
      return Color.blue
    } else {
      return Color.orange
    }
  }
}
egeeke
  • 753
  • 1
  • 6
  • 18
  • Thank you for your reply, i want also change colour with like text 1 red, text 2 blue etc.. is it possible? – Sham Dhiman Mar 01 '21 at 16:34
  • Yes, it is possible. Please see edited answer, and if it's okay for you please accept my answer. – egeeke Mar 01 '21 at 16:47
  • You are right, i have implemented your code but it is not showing properly image from top and bottom can you please check my screenshot https://i.stack.imgur.com/Br8E6.png – Sham Dhiman Mar 01 '21 at 16:51
  • Don't forget to add `.ignoresSafeArea()` after your image. If you look at my code you will see that after `Color` – egeeke Mar 01 '21 at 16:53