The text gets upon the image.Why this is happening?
In my opinion this is what happens:
The image placeholder container is rendered with a frame height of 300
The text is rendered below the container
The asynchronous image downloads and is set with a .aspectRatio(contentMode: .fill) modifier
If the downloaded image, modified to fill the container, end up with a height over 300 it will spill out from the container and render below the text.
What you can do is to center crop your image:
//For your specific case
image
.resizable()
.scaledToFill()
.frame(width: 300, height: 300, alignment: .center)
.clipped()
Related
I am trying to achieve a Scrollview with buttons that have rounded corners and a custom color.
Button(shoppingListItem.text) {
removeFromShoppingList(itemId: shoppingListItem.item_id)
}
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.secondary, lineWidth: 2)
.background(
RoundedRectangle(cornerRadius: 20, style: .continuous)
.fill(Color("ShoppingListItemColor"))
)
)
)
This results in a button with rounded corners and the wanted color but no text is visible. What am I missing here?
You might be better writing your code this way...
Button(shoppingListItem.text) {
removeFromShoppingList(itemId: shoppingListItem.item_id)
}
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.secondary, lineWidth: 2)
)
.background(Color("ShoppingListItemColor"))
.clipShape(RoundedRectangle(cornerRadius: 20))
In this I have moved the background out of the overlay.
So this is now built from a button with text.
The button has an overlay of just the border of a rounded rectangle.
Then the button has a background color.
And then the button is clipped by a rounded rectangle.
In your code (probably because of the lack of formatting) you had inadvertently added the background to the overlay. So you had overlaid the whole button with the background colour and covered the text.
Using iOS14.5, Swift5.4, XCode12.5, and SwiftUI,
I am trying to build simple DetailView to which you can navigate to using a NavigationLink.
It all works, except that I have no idea about the height of my NavigationBar inside the DetailView. (note that I use the .inline Version by calling .navigationBarTitleDisplayMode(.inline) on the DetailView).
I am trying to show a simple two-line VStack in the DetailView.
And it turns out that the DetailView's body is covered by the NavigationBar unless I move the whole thing down by 56 Pixels (by using Spacer().frame(height: 56)).
And I don't even know the precise height of the NavigationBar
And even worse, I don't know how to determine it dynamically for Portrait and Landscape rotations.
How do you make the DetailView's body start below the NavBar no matter rotation-states ?
Here example Screenshots for Portrait :
Left side: the title is cut off ! Right side: using a Spacer makes title appear
Here is how I open the navigationLink:
NavigationLink(
destination: DetailView(session: session).navigationBarTitleDisplayMode(.inline),
tag: session.id ?? "",
selection: $selectedTag,
label: { EmptyView() }
)
And here is the entire Detail-Screen:
(please notice the Spacer-distance of 56 in order to make the title visible at all)
import SwiftUI
struct DetailView: View {
#State var session: THSession
var body: some View {
VStack(alignment: .leading) {
Spacer().frame(height: 56) // !!!!!!!!!! without this the title sits behind NavBar !!!!!!!!!!!!!!!!!!!
Text(session.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
Spacer().frame(height: 16)
HStack {
Text(session.invitationCode)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
}
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.background(Color.red)
.ignoresSafeArea()
}
}
I'm assuming that you're using ignoresSafeArea() because you want the background to extend into the safe area. But, where you have it placed is having a side effect of making the titles extend into the nav bar area.
Instead, place ignoresSafeArea() inside the background modifier:
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.background(Color.red.ignoresSafeArea())
Now, your titles won't extend into the safe area and you can avoid using a Spacer at the top of the View altogether.
If I have a Label that uses a system image and some text, let's call the text "title". How would I align the subtitle to the the leading edge of the title text and not the image?
This seems to work, but is there another way? Something just seems off about setting the top alignment.
HStack(alignment: .top) {
Image(systemName: "gift")
VStack(alignment: .leading) {
Text("Title")
Text("Subtitle")
}
}
I have added custom target as a framework to use in my codebase. I can't see image in preview canvas. When I run in simulator, image is there.
VStack(alignment: .leading) {
Text("Hello World")
Image("icon_star")
.resizable()
.renderingMode(.original)
.frame(width: 14, height: 14)
}.background(Color.white)
Any suggestion
Thanks
I'm studying SwiftUI and when I using TextField then I have a problem with TextField's cursor is still at left-center of TextField's area.
How can I set it to left-top of TextField area.
Thanks!
This is my code:
TextField("What is your mind?", text: $statusContent)
.clipped()
.frame(height: 100, alignment: .leading)
.multilineTextAlignment(.leading)