I read tons of questions about how to make the app ignore the safe area, but when I create a new app then the status bar space is ignored anyway without
.ignoresSafeArea()
Why is that? I don't want it to be ignored!
This is all I have:
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Spacer()
}
.frame(width: 300)
.background(Color.teal)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
the result screenshot
You can do it like so:
VStack {
Color.teal
.overlay {
Text("Hello World!")
}
}
Ok I got it, inside a ZStack:
GeometryReader { reader in
Color.white
.frame(height: reader.safeAreaInsets.top, alignment: .top)
.ignoresSafeArea()
}
Use this modifier:
.edgesIgnoringSafeArea(.all)
if you don't want everything you can choose : .top, .bottom, .vertical, .horizontal,...
Related
I had tried this code, I have also tried with stacks but not able to align with top .
I just want the view to appear on top with auto layout as like UIKit .
import SwiftUI
struct ItemDetail: View {
let item : MenuItem
var body: some View {
HStack(alignment: .top){
VStack{
Image(item.mainImage)
Text(item.description)
}
.padding(10)
.background(.red)
.navigationTitle(item.name)
}
}
}
struct ItemDetail_Previews: PreviewProvider {
static var previews: some View {
ItemDetail(item: MenuItem.example)
}
}
Swap the HStack for a VStack and add a Spacer() at the end. e.g.:
struct ItemDetail: View {
let item : MenuItem
var body: some View {
VStack{
VStack{
Image(item.mainImage)
Text(item.description)
}
.padding(10)
.background(.red)
.navigationTitle(item.name)
Spacer()
}
}
}
This should do it:
VStack()
{
VStack
{
Image(item.mainImage)
Text(item.description)
}
.padding(10)
.background(.red)
.navigationTitle(item.name)
Spacer()
}
The default "Keyboard Avoidance" SwiftUI is used.
First GIF
If you put the code in VStack and add another View to it, then the container rises
Second GIF
I don't want to delete Keyboard Avoidance. I need to remove extra spacing
scrollDismissesKeyboard for ScrollView is not an option
minimal iOS version is iOS 16
struct ContentView: View {
#State var text: String = "Bu bu?"
var body: some View {
NavigationStack {
ScrollViewReader { proxy in
VStack {
ScrollView(showsIndicators: false) {
VStack(spacing: 0) {
Spacer()
.frame(height: 500)
TextField("", text: $text)
.padding(.bottom, 70)
.frame(height: 40)
.frame(maxWidth: .infinity)
.background(Color.red)
NavigationLink("Screen 2", destination: {
Text("SwiftUI - Nice to meet you, let's never meet again")
})
}
}
Text("I'm in VSTack after scroll view")
}
}
}
}
}
I looked it up with a hierarchy view, and noticed that a UIInputSetHostView is created with a height of 216
View hierarchy 1
View hierarchy 2
disableAutocorrection not working
Is there a simple way to place text relatively between the top and center of the screen? With 'simple' I mean preferably without GeometryReader and overt calculations.
I've tried various combinations of VStack, Zstack and Spacer() but to no avail.
Code and screenshot as illustration:
struct ContentView: View {
var body: some View {
Text("Between top and center")
.offset(y: -150)
Text("Center")
}
}
A possible solution is to create two views on each side of the centred text (one hidden):
struct ContentView: View {
var body: some View {
VStack {
Spacer()
Text("Between top and center")
Spacer()
Text("Center")
Spacer()
Text("")
Spacer()
}
}
}
Alternatively, to be fool-proof (in case they change the behaviour of Text("") in the future release):
struct ContentView: View {
var body: some View {
VStack {
Spacer()
Text("Between top and center")
Spacer()
Text("Center")
Spacer()
Text("Between top and center")
.opacity(0)
Spacer()
}
}
}
(If needed, Text("Between top and center") can be extracted as a subview to avoid code duplication.)
As you said:
Spacer() [...] ensure[s] that the empty spaces between the views are equal in size
That's exactly the reason why I recommended to extract Text("Between top and center") as a subview. Then, you can change the font, size etc. of the view in one place only.
The adapted solution is:
struct ContentView: View {
var body: some View {
ZStack {
VStack {
Spacer()
subview
Spacer()
Spacer()
subview
.opacity(0)
Spacer()
}
VStack {
Spacer()
Text("Center")
Spacer()
}
}
}
var subview: some View {
Text("Between top and center")
.font(.caption)
// apply all other modifiers if you want
}
}
Here is possible solution (tested with Xcode 12.1 / iOS 14.1)
struct ContentView: View {
var body: some View {
VStack {
Color.clear.overlay(
Text("Between top and center") // << here !!
)
// Text("Between top and center")
// .frame(maxWidth: .infinity, maxHeight: .infinity) // alternate !!
Text("Center")
Color.clear
}
}
}
Update: added alternate with .frame but it is valid only if there should be the only one Text, whereas in .overlay it is possible to put anything.
Here is another solution using the method of VStack Spacers, I also used a little padding to get you closer to the center.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Top").bold()
.foregroundColor(.blue)
Spacer()
Text("Your desired area")
Spacer()
Text("Middle Text").bold()
.foregroundColor(.blue)
.padding(.bottom, 200)
Spacer()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I'm trying to reproduce the Apple tutorial(Composing Complex Interfaces) and I have a very weird problem. My CategoryItem view is being displayed as a blue frame.
If I remove the NavigationLink which wraps it, everything works fine but with that one it doesn't.
struct CategoryRow: View {
var categoryName: String
var items: [Landmark]
var body: some View {
VStack(alignment: .leading) {
Text(self.categoryName)
.font(.headline)
.padding(.leading, 15)
.padding(.top, 5)
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 0) {
ForEach(self.items) { landmark in
NavigationLink(
destination: LandmarkDetail(
landmark: landmark
)
) {
CategoryItem(landmark: landmark)
}
}
}
}.frame(height: 185)
}
}
}
NavigationLink has a blue accent color by default, just call .accentColor(Color.clear) on it
Or you could try this:
NavigationView {
NavigationLink(destination: Text("Detail view here")) {
Image("YourImage")
}
.buttonStyle(PlainButtonStyle())
}
https://www.hackingwithswift.com/quick-start/swiftui/how-to-disable-the-overlay-color-for-images-inside-button-and-navigationlink
renderingMode(.original) is what did it for me; .accentColor(Color.clear) made the image invisible (my best explanation here is because it didn't have a transparency).
NavigationView {
NavigationLink(destination: Text("Detail view here")) {
Image("YourImage")
.renderingMode(.original)
}
}
As the answer above mentioned, How to disable the overlay color for images inside Button and NavigationLink is a good write up as well.
When adding a TabView in my SwiftUI iOS App, the Navigation Bar stops covering up the notch
I've tried creating another file for the TabView implementation ( Modifying SceneDeletage and so on)
Here is a simple code without TabView that makes the Navigation Bar cover the safe area (aka the notch)
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView{
ScrollView{
HStack{
VStack{
ForEach((1...10), id: \.self){_ in
Text("Hello")
.padding(.leading, 20)
}
}
Spacer()
//.padding(.leading, 20)
}
}
.navigationBarTitle("Title Covers Safe Area")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here is a code with TabView that makes the Navigation Bar NOT cover up the safe area
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
NavigationView{
ScrollView{
HStack{
VStack{
ForEach((1...10), id: \.self){_ in
Text("Hello")
}
}
Spacer()
}
.padding(.leading, 20)
}
.navigationBarTitle("Doesn't Cover Safe Area")
}
.tabItem {
Image(systemName: "1.circle")
Text("First")
}.tag(0)
HStack{
Spacer()
VStack{
Spacer()
Text("Second View")
.font(.system(size: 40))
}
}
.tabItem {
Image(systemName: "2.circle")
Text("Second")
}.tag(1)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You can use method edgesIgnoringSafeArea(_:)
TabView {
...
}
.edgesIgnoringSafeArea(.top)