Make Text inside VStack take the whole width of the VStack - swift

I'm trying to make two Text that are inside a VStack to take the whole width of the parent (the VStack).
I'm completely out of ideas.
This is what I have:
var body: some View {
VStack(alignment: .center, spacing: 0) {
Image("image")
.resizable()
.scaledToFill()
.frame(height: 190)
.clipped()
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.title)
.background(Color.red)
.fixedSize(horizontal: false, vertical: true)
Text(subtitle)
.font(.subheadline)
.background(Color.yellow)
.fixedSize(horizontal: false, vertical: true)
}
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.green)
.padding(.top, 24)
.padding(.horizontal, 24)
Button(buttonTitle) { print("Something") }
.frame(maxWidth: .infinity, minHeight: 56)
.background(Color.red)
.foregroundColor(.white)
.cornerRadius(56/2)
.padding(.top, 32)
.padding(.horizontal, 24)
.padding(.bottom, 20.0)
}.background(Color.blue)
.cornerRadius(38)
.frame(minWidth: 0, maxWidth: UIScreen.main.bounds.size.width - 48)
}
And this is how it looks:
I want the red and the yellow label to take the full width of the green VStack.
How can I achieve that?!

Remove the .fixedSize on both Text
Add .frame(minWidth: 0, maxWidth: .infinity) instead
In SwiftUI, it also matters the order of things, so you'll want to add the .background to after the .frame
Final result would be:
Text("title")
.font(.title)
.frame(minWidth: 0, maxWidth: .infinity)
.background(Color.red)
To instead make it left aligned:
HStack {
Text("title")
.font(.title)
Spacer(minLength: 0)
}
.background(Color.red)

Here is possible solution
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.title)
.frame(maxWidth: .infinity) // << here
.background(Color.red)
.fixedSize(horizontal: false, vertical: true)
Text(subtitle)
.font(.subheadline)
.frame(maxWidth: .infinity) // << here
.background(Color.yellow)
.fixedSize(horizontal: false, vertical: true)
}
.frame(maxWidth: .infinity) // << here

Related

How to remove big blank space on my list pagination [SwiftUI]

I am new to SwiftUI and I trying to remove the blank space on my list pagination. Everytime when the list loaded the new page, a blank space is pop-up. how can I fix this?
You can see the problem by bellow attached image,
Screenshot
this is my list view
My List
List {
LazyVGrid(columns: setGridLayout(), spacing: 20) {
ForEach(viewModel.personPopularList, id: \.id) { person in
PeopleCardView(person: person)
.listRowBackground(Color.white)
.listRowSeparator(.hidden)
}
.id(UUID())
.frame(maxWidth: .infinity)
}
.listRowBackground(Color.white)
.listRowSeparator(.hidden)
}
.frame(maxWidth: .infinity)
.onAppear(perform: {
UITableView.appearance().contentInset.top = -20
UITableView.appearance().backgroundColor = UIColor.white
})
.listStyle(GroupedListStyle())
this is my cardview
My CardView
struct PeopleCardView: View {
#State var person: PersonPopularResult
var body: some View {
ZStack(alignment: .center) {
HStack(alignment: .center) {
WebImage(url: URL(string: Constant.POSTER_URL + (person.profilePath ?? "")))
.placeholder {
Image("placeholder")
}
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 100, height: 100)
.clipShape(RoundedRectangle(cornerRadius: 10))
.overlay(RoundedRectangle(cornerRadius: 10)
.stroke(Color.mPurple, lineWidth: 3))
Spacer()
VStack(alignment: .leading) {
Text("\(person.name!)")
.fontWeight(.bold)
.lineLimit(1)
.font(.system(size: 21))
.font(.headline)
Text("Popolarity Rate: \(String(format: "%.3f", person.popularity!))")
.fontWeight(.bold)
.foregroundColor(.mPurple)
.lineLimit(1)
.font(.system(size: 14))
.padding(.top, -8)
Spacer()
}
.padding([.top, .leading], 10)
Spacer()
}
.frame(width: 300, height: 100, alignment: .center)
.padding()
.background(
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(.white)
.overlay(RoundedRectangle(cornerRadius: 10)
.stroke(Color.mPurple, lineWidth: 3))
.shadow(radius: 7)
)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
thanks for anyone can help to my problem.

Pagination is not working on LazyVGrid layout but without LazyVGrid the pagination is working properly. How can i fix this?

Pagination is not working on LazyVGrid layout, but without LazyVGrid the pagination is working properly. I really need to use LazyVGrid for my list. How can I fix this?
My code
VStack(alignment: .center) {
List {
LazyVGrid(columns: gridItemLayout, spacing: 20) {
ForEach(viewModel.tvShowList, id: \.id) { tvShow in
VStack {
KFImage.url(URL(string: Constant.POSTER_URL + tvShow.posterPath)!)
.placeholder {
Image("placeholder")
.scaledToFill()
.frame(width: 160, height: 100, alignment: .center)
.clipped()
}
.resizable()
.shadow(radius: 10)
.padding(.trailing, 20)
.scaledToFit()
.frame(width: 160, alignment: .center)
Text("\(tvShow.name)")
.multilineTextAlignment(.center)
.lineLimit(1)
.frame(maxWidth: .infinity)
.font(.system(size: 14))
.font(.headline)
}
.listRowBackground(Color.white)
.listRowSeparator(.hidden)
}
.id(UUID())
.frame(maxWidth: .infinity)
if (viewModel.page < viewModel.totalPage) {
HStack {
Spacer()
ProgressView()
.onAppear {
viewModel.page += 1
viewModel.getTvShowTopRated()
}
Spacer()
}
}
}
}
.listStyle(GroupedListStyle())
.padding([.leading, .trailing], 15) }

SwiftUI getting SF image inside button

I have the following button and code, but cannot get the SF image to go on top of the button. I'd like it to be on the left but on top of the gradient - how do I achieve this please?
NavigationLink(destination: MyView(viewModel: FairRentViewModel(Amounts(), people: 2)))
{
Image(systemName: "person.crop.circle.badge.plus")
.font(.largeTitle)
Text("2")
.fontWeight(.bold)
.font(.title)
.padding()
.frame(minWidth: 0 maxWidth: 250, alignment: .center)
.background(gradient)
.foregroundColor(.white)
.padding(2)
.overlay(
RoundedRectangle(cornerRadius:20)
.stroke(gradient, lineWidth:15)
)
.padding(5)
}
Put it into overlay
Text("2")
.fontWeight(.bold)
.font(.title)
.padding()
.frame(minWidth: 0 maxWidth: 250, alignment: .center)
.background(gradient)
.foregroundColor(.white)
.padding(2)
.overlay(
RoundedRectangle(cornerRadius:20)
.stroke(gradient, lineWidth:15)
)
.overlay( // << here !!
Image(systemName: "person.crop.circle.badge.plus")
.font(.largeTitle)
//.padding(.leading) // might be needed
, alignment: .leading)
.padding(5)
You could try to put it inside an HStack.

Scrollview alignment

I have the following:
VStack(alignment: .leading) {
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .leading, spacing: 30) {
ForEach(0 ..< meals.count) { count in
HStack(alignment: .center,spacing: 120){
VStack{
Text("\(count + 1) ")
.foregroundColor(Color.black)
.padding(.horizontal, 8)
.padding(.vertical, 5)
.background(
Capsule()
.fill(Color.black)
.opacity(0.20)
)
}
VStack{
Text("\(self.meals[count]) ")
.foregroundColor(Color.black)
}
VStack{
Image(systemName: "pencil")
.foregroundColor(Color.black)
.font(.system(size: 20))
.background(Color.yellow)
}
}
}
}
.frame(width: UIScreen.main.bounds.width) // set a fixed width
// .background(Color.green)
}
.frame(height: 185)
.frame(maxWidth: 400)
.padding(.top, -200)
}
How can I make the pencil icon stay to the right? I tried putting alignment: .trailing on it but doesn't work.
I saw an example with geometry but then the scroll view gets weird, I need it with this. Also how can I make sure the width between the items stay the same across devices?
Instead of hardcoding spacing use Spacer() which like spring push subviews aside.
Note: You don't need VStack for only one subview, so it can be removed for simplicity.
Here is resulting row:
HStack { // .center is by default, so also remove
Text("\(count + 1) ")
.foregroundColor(Color.black)
.padding(.horizontal, 8)
.padding(.vertical, 5)
.background(
Capsule()
.fill(Color.black)
.opacity(0.20)
)
Spacer() // << here !!
Text("\(self.meals[count]) ")
.foregroundColor(Color.black)
Spacer() // << here !!
Image(systemName: "pencil")
.foregroundColor(Color.black)
.font(.system(size: 20))
.background(Color.yellow)
}

SwiftUI multiple TextField causing lags

I am using many TextFields in my SwiftUI Mac OS app. I am a main form, where the user adds data, which is composed of multiple Stacks/ views which contain TextFields/ and Text()
I noticed a strange bug, when I am using many TextFields combined in different views, I get some very heavy lags when selecting the TextField. Also when I skip to the next one with Tab it takes time.
It is really strange behavior and I am not sure what is causing that. I have a different view with many TextFields aswell, where everything is working smooth.
Also sometimes I can not jump back to the prior TextField with Shift+Tab.
Edit: I could figure out how to get rid of it. However, only if I remove one view. I don't want to.
My main container looks like that:
HStack
{
//Left Side
VStack{
Text("Here is the next #1")
}
VStack
{
//The View posted below
ViewBelow()
}
}
If I remove the first VStack, the issue is solved. Nothing lagging. If I add it, it is there again.
Just to give you a neckline of my code (many Stacks with TextFields)
ZStack
{
VStack
{
HStack
{
Text("Personal Data")
.font(.title)
.fontWeight(.bold)
.padding(.horizontal, 0)
.padding(.vertical, 5)
Spacer()
}
Divider()
.padding(.bottom, 5)
//Personal Data Types
HStack(spacing: 0)
{
//Left Element
VStack(alignment: .center)
{
Text("Vorname")
.foregroundColor(Color.gray)
} .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
//Right Elemenet
VStack()
{
Text("Nachname")
.foregroundColor(Color.gray)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}.frame(minWidth: 0, maxWidth: .infinity)
HStack(spacing: 0)
{
VStack(alignment: .center)
{
//Left Element
if (self.editMode)
{
TextField("Enter some text", text: $s_test)
.frame(maxWidth:250)
}
else
{
Text(self.person.firstName)
.font(.body)
}
} .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
VStack()
{
//Left Element
if (self.editMode)
{
TextField("Enter some text", text: self.$s_test)
.frame(maxWidth:250)
}
else
{
Text(self.person.lastName)
.font(.body)
}
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}.frame(minWidth: 0, maxWidth: .infinity)
.padding(.top, 0)
//Personal Data Types
HStack(spacing: 0)
{
//Left Element
VStack(alignment: .center)
{
Text("Nickname")
.foregroundColor(Color.gray)
} .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
//Right Elemenet
VStack()
{
Text("Birthday")
.foregroundColor(Color.gray)
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}.frame(minWidth: 0, maxWidth: .infinity)
.padding(.top, 10)
HStack(spacing: 0)
{
VStack(alignment: .center)
{
//Left Element
if (self.editMode)
{
TextField("Enter some text", text: self.$s_test)
.frame(maxWidth:250)
}
else
{
Text(self.person.nickname)
.font(.body)
}
} .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
VStack()
{
//Right Element
if (self.editMode)
{
TextField("Enter some text", text: self.$s_test)
.frame(maxWidth:250)
}
else
{
Text(self.person.birthdate)
.font(.body)
}
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}.frame(minWidth: 0, maxWidth: .infinity)
.padding(.top, 0)
}
Maybe someone experienced the same already.. I am not quiet sure how to solve it. I removed many views, then it is working better. However I am not sure if it is just the quantity of TextFields or some bug in the other view.
I demonstrate the lag in the Gif. I press instantly when I hover above the TextField. See that lag..