I have two Hstack inside Vstack but Elements of Hstack are not distributed equally due to text length.
Due to Current task and Completed Task Text length my Hstack do not equally distribute. How can I achieved equally distribution?
I have to use VSTACK and Inside it HSTACK and inside it three Vstack with image and text.
Similarly I have another Hstack and inside it three VSTACK with image and text but element inside my HSTACK do not distributed due to the length of text.
VStack{
VStack{
HStack{
Spacer()
VStack{
availableFuntionView(id:0, imageName: "home_icon_new_taks")
Text("New Task's")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
Spacer()
VStack{
availableFuntionView(id:1, imageName: "home_icon_current_taks")
Text("CURRENT TASK")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
Spacer()
VStack{
availableFuntionView(id:2, imageName: "home_icon_completed_tasks")
Text("COMPLETED TASK")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
Spacer()
}
}
VStack{
HStack{
Spacer()
VStack{
availableFuntionView(id:3, imageName: "home_icon_chat")
Text("CHAT")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
Spacer()
VStack{
availableFuntionView(id:4, imageName: "home_icon_settings")
Text("SETTING")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
Spacer()
VStack{
availableFuntionView(id:2, imageName: "home_icon_logout")
Text("LOGOUT")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
Spacer()
}
}
Spacer().frame(minHeight: 20, maxHeight: .infinity)
}
I hope my question is clear. Please help me out. I will be very thankful.
You need to use a GeometryReader. This will give you the screen size of whatever device you are currently running on. Now, this is probably not an ideal set up as you could end up with shortened text. I am not sure how much you are trying to put on the screen at once...
GeometryReader { geometry in
VStack{
VStack{
HStack{
VStack{
availableFuntionView(id:0, imageName: "home_icon_new_taks")
Text("New Task's")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
.frame(width: (geometry.size.width / 4))
VStack{
availableFuntionView(id:1, imageName: "home_icon_current_taks")
Text("CURRENT TASK")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
.frame(width: (geometry.size.width / 4))
VStack{
availableFuntionView(id:2, imageName: "home_icon_completed_tasks")
Text("COMPLETED TASK")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
.frame(width: (geometry.size.width / 4))
}
}
VStack{
HStack{
Spacer()
VStack{
availableFuntionView(id:3, imageName: "home_icon_chat")
Text("CHAT")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
.frame(width: (geometry.size.width / 4))
VStack{
availableFuntionView(id:4, imageName: "home_icon_settings")
Text("SETTING")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
.frame(width: (geometry.size.width / 4))
VStack{
availableFuntionView(id:2, imageName: "home_icon_logout")
Text("LOGOUT")
.foregroundColor(.white)
.font(.system(size: UIScreen.main.bounds.width * 0.04))
.minimumScaleFactor(0.5)
.lineLimit(1)
}
.frame(width: (geometry.size.width / 4))
}
}
Spacer().frame(minHeight: 20, maxHeight: .infinity)
}
}
}
You will have to play with the multiplier some for it to look the way you want, but this is the general idea.
Related
How can I force the text to always align to the left regardless of how big is the text?
What I get is what's shown in the first image, what I need is to align it to look like the second image.
struct GeneralTest: View {
let fruits = ["Apple", "Oranges from Californ"]
var body: some View {
List {
ForEach(fruits, id: \.self) { fruit in
HStack{
Image(systemName: "pencil.circle.fill")
.frame(width: 65)
.foregroundColor(.blue)
.background(.yellow)
VStack(alignment: .leading){
Text(fruit)
.font(.title3)
.foregroundColor(Color.blue)
VStack(alignment: .leading) {
Text("Some text")
.foregroundColor(Color.gray)
.font(.caption)
Text("Some text:")
.foregroundColor(Color.gray)
.font(.caption)
Text("Some Text:")
.foregroundColor(Color.gray)
.font(.caption)
}
}
.frame(maxWidth: .infinity)
.background(.yellow)
Button{
}label:{
Image( systemName: "circle.hexagongrid.circle")
.padding()
.background(Color.yellow)
.foregroundColor( Color.blue)
.cornerRadius(30)
.overlay(
Circle()
.stroke( Color.gray, lineWidth: 0.5)
)
}
NavigationLink(destination: SomeView()){
}
.frame(maxWidth: 10)
}
} // ForEach end
}
.background(Color.pink)
.listStyle(GroupedListStyle())
}
}
Wrap the "fruit" VStack inside an HStack, then use a Spacer() to move the text to the left.
Like this:
ForEach(fruits, id: \.self) { fruit in
HStack{
Image(systemName: "pencil.circle.fill")
.frame(width: 65)
.foregroundColor(.blue)
.background(.yellow)
// The VStack is inside an HStack
HStack {
VStack(alignment: .leading){
Text(fruit)
.font(.title3)
.foregroundColor(Color.blue)
VStack(alignment: .leading) {
Text("Some text")
.foregroundColor(Color.gray)
.font(.caption)
Text("Some text:")
.foregroundColor(Color.gray)
.font(.caption)
Text("Some Text:")
.foregroundColor(Color.gray)
.font(.caption)
}
}
// This Spacer() will push the VStack to the left
Spacer()
}
.frame(maxWidth: .infinity)
.background(.yellow)
Button{
}label:{
Image( systemName: "circle.hexagongrid.circle")
.padding()
.background(Color.yellow)
.foregroundColor( Color.blue)
.cornerRadius(30)
.overlay(
Circle()
.stroke( Color.gray, lineWidth: 0.5)
)
}
NavigationLink(destination: SomeView()){
}
.frame(maxWidth: 10)
}
} // ForEach end
I am trying to prevent the background of my HStack from going into my safearea.
There is a ZStack that is setting the background color. I am using green just to be more defined for now.
struct AccountView: View {
var body: some View {
ZStack{
Color(.green)
.ignoresSafeArea()
VStack(spacing: 32){
HStack {
Image(systemName: "person")
.resizable()
.scaledToFill()
.frame(width: 64, height: 64)
.clipShape(Circle())
.padding(.leading)
VStack(alignment: .leading, spacing: 4) {
Text("First Last")
.font(.system(size: 18))
Text("Available")
.foregroundColor(.gray)
.font(.system(size: 14))
}
Spacer()
}
.frame(height: 80)
.background(Color("ItemBG"))
Spacer()
}
}
}
}
We need to use shape as background instead of pure-colour, then it is not spread outside.
Here is simplified demo. Tested with Xcode 13.2 / iOS 15.2
var body: some View {
VStack {
HStack {
Text("Hello")
}
.frame(maxWidth: .infinity, maxHeight: 80)
.background(Rectangle().fill(.yellow)) // << here !!
Spacer()
}
.background(Color.green.ignoresSafeArea())
}
I was able to figure it out...
I had to give by HStack a padding of 1 on the top. Apparently with IOS 15 if it touches the safe area it bleeds.
HStack {
Image(systemName: "person")
.resizable()
.scaledToFill()
.frame(width: 64, height: 64)
.clipShape(Circle())
.padding(.leading)
VStack(alignment: .leading, spacing: 4) {
Text("First Last")
.font(.system(size: 18))
Text("Available")
.foregroundColor(.gray)
.font(.system(size: 14))
}
Spacer()
}
.frame(height: 80)
.background(Color("ItemBG"))
.padding(.top, 1)
how are you ?
I need to fit all the content of first zstack (I use two zstack to add a transparent layer over first zstack background) to window size and if the user scroll down, shows additional stacks added below.
I add Spacer() to push the last HStack to bottom and before stacks to the top (in middle must be a dynamic blank space) of window without result.
Can help me please ?
Thanks
ScrollView(.vertical) {
ZStack {
bgColors[weatherDescription]
ZStack {
LinearGradient(gradient: Gradient(colors: [Color(hex: 0x212121, alpha: 0.5), Color(hex: 0x212121, alpha: 0.5)]), startPoint: .top, endPoint: .bottom)
VStack(alignment: .leading) {
Text("Hello!")
.foregroundColor(Color.white)
.fontWeight(.heavy)
.font(.largeTitle)
.padding(.top, 20)
.padding(.bottom, 1)
Text("Hello!")
.foregroundColor(Color.white)
.font(.title)
.padding(.bottom, 1)
Text("Hello!")
.foregroundColor(Color.white)
.font(.title3)
.padding(.bottom, 20)
HStack(alignment: .center, content: {
Text("TEXT1")
.foregroundColor(Color.white)
.font(.title3).fontWeight(.bold)
.frame(maxWidth: .infinity, alignment: .leading)
Text("TEXT2")
.foregroundColor(Color.white)
.font(.title3).fontWeight(.bold)
.frame(maxWidth: .infinity, alignment: .trailing)
}).frame(maxWidth: .infinity)
Spacer()
HStack(alignment: .center, spacing: 30, content: {
Text("Text1")
.foregroundColor(Color.white)
.font(.system(size: 90))
Text("Text2")
.foregroundColor(Color.white)
.font(.caption)
.animation(.easeIn)
}).frame(width: .infinity, alignment: .bottom)
}.padding() // vstack
}.edgesIgnoringSafeArea(.top) // zstack
}.edgesIgnoringSafeArea(.top) // zstack
// ADITIONALS STACKS VISIBLES ONLY WHEN SCROLL DOWN
}
You just used so many codes, I edited them to less code, here for example you just used so many foregroundColor which you can see in my code I used just one time on parent View.
Also we can not use ScrollView like you used it! ScrollView needs Content to Scroll, you have less Content.
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
LinearGradient(gradient: Gradient(colors: [Color.red, Color.purple]), startPoint: .top, endPoint: .bottom)
.ignoresSafeArea()
VStack(alignment: .leading, spacing: 30) {
Text("Hello!")
.fontWeight(.heavy)
.font(.largeTitle)
Text("Hello!")
.font(.title)
Text("Hello!")
.font(.title3)
HStack {
Text("TEXT1")
.font(.title3)
.fontWeight(.bold)
Spacer()
Text("TEXT2")
.font(.title3)
.fontWeight(.bold)
}
Spacer()
HStack {
Text("Text1")
.font(.system(size: 90))
Spacer()
Text("Text2")
.font(.caption)
}
}
.padding()
}
.foregroundColor(Color.white)
.animation(.easeIn)
}
}
I have made a login view in SwiftUI and it worked at first but now that I have styled the view, my textfields don't work anymore. I can't type anything in the Textfields. Is this a Xcode bug or have I made a mistake. This is the code:
ZStack{
Color.red.ignoresSafeArea()
VStack {
Image("login")
.resizable()
.frame(width: 150, height: 150)
.shadow(radius: 5)
.padding(.top, 20)
.offset(y: -50)
Spacer()
}
VStack {
Spacer()
VStack {
HStack {
Image(systemName: "mail")
TextField("Email", text: $email)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.padding(.horizontal)
.padding(.top, 30)
HStack {
Image(systemName: "lock.rectangle")
SecureField("Password", text: $password)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.padding(.horizontal)
.zIndex(1)
Button(action: {login()}) {
Text("Sign in")
.padding()
.foregroundColor(.white)
.frame(width: UIScreen.main.bounds.width - 100, height: 50)
.background(Color.red)
.cornerRadius(10)
.padding(.horizontal)
.padding(.top)
}.buttonStyle(PlainButtonStyle())
NavigationLink(destination: Register()) {
Text("No account? Sign up!")
.padding()
.foregroundColor(.black)
.cornerRadius(35)
.padding(.horizontal)
}.buttonStyle(PlainButtonStyle())
Spacer()
}
.frame(height: UIScreen.main.bounds.height * 0.6)
.background(RoundedCorners(color: .white, tl: 30, tr: 30, bl: 0, br: 0))
.ignoresSafeArea()
.offset(y: 35)
}.shadow(radius: 15)
}
The login button and register text do work so I don't think that there is an invisible object above the textfields.
This is my design
First, you are using three views insid the ZStack. In other words you are laying three layers over the others. You should use 2 views inside the Zstack.
Second, use the clipped() modifier before shadow().
ZStack{
Color.red
VStack {
Spacer()
Image(systemName: "plus")
.resizable()
.frame(width: 150, height: 150)
.shadow(radius: 5)
.padding(.top, 20)
.offset(y: -50)
VStack {
HStack {
Image(systemName: "mail")
TextField("Email", text: $email)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.padding(.horizontal)
.padding(.top, 30)
HStack {
Image(systemName: "lock.rectangle")
SecureField("Password", text: $password)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.padding(.horizontal)
Button(action: {}) {
Text("Sign in")
.padding()
.foregroundColor(.white)
.frame(width: UIScreen.main.bounds.width - 100, height: 50)
.background(Color.red)
.cornerRadius(10)
.padding(.horizontal)
.padding(.top)
}.buttonStyle(PlainButtonStyle())
NavigationLink(destination: Text("hello")) {
Text("No account? Sign up!")
.padding()
.foregroundColor(.black)
.cornerRadius(35)
.padding(.horizontal)
}
.buttonStyle(PlainButtonStyle())
Spacer()
}
.frame(height: UIScreen.main.bounds.height * 0.6)
.background(Color.white)
.clipped()
.shadow(radius: 15, x: 0, y: 0)
}
}
.ignoresSafeArea()
However, using the ZStack is unnecessary.
VStack {
Spacer()
Image(systemName: "plus")
.resizable()
.frame(width: 150, height: 150)
.shadow(radius: 5)
.padding(.top, 20)
.offset(y: -50)
VStack {
HStack {
Image(systemName: "mail")
TextField("Email", text: $email)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.padding(.horizontal)
.padding(.top, 30)
HStack {
Image(systemName: "lock.rectangle")
SecureField("Password", text: $password)
}
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
.padding(.horizontal)
Button(action: {}) {
Text("Sign in")
.padding()
.foregroundColor(.white)
.frame(width: UIScreen.main.bounds.width - 100, height: 50)
.background(Color.red)
.cornerRadius(10)
.padding(.horizontal)
.padding(.top)
}.buttonStyle(PlainButtonStyle())
NavigationLink(destination: Text("hello")) {
Text("No account? Sign up!")
.padding()
.foregroundColor(.black)
.cornerRadius(35)
.padding(.horizontal)
}
.buttonStyle(PlainButtonStyle())
Spacer()
}
.frame(height: UIScreen.main.bounds.height * 0.6)
.background(Color.white)
.clipped()
.shadow(radius: 15, x: 0, y: 0)
}
.background(Color.red)
.ignoresSafeArea()
You should use .background(RoundedCorners(color: .white, tl: 30, tr: 30, bl: 0, br: 0)) adjust the offset of the innner Vstack.
This has happened to me before. Usually it has just been an Xcode glitch and I have to quit and restart the IDE a couple times before it actually shows up. However in Simulator it works. Hope it's resolved now though! :)
How can I restrict the view to resize only downwards?
I am trying to add more buttons & have the view resize to accommodate the buttons without overlapping the red rectangle above.
This is the code I am currently using:
struct ContentView: View {
var body: some View {
VStack{
RoundedRectangle(cornerRadius: 20)
.frame(width: .infinity, height: 55)
.foregroundColor(.red)
HStack{
Spacer()
ZStack{
RoundedRectangle(cornerRadius: 20)
.foregroundColor(.green)
VStack{
Button(action: {}) {
Text("Test Button")
.foregroundColor(.white)
.font(.headline)
.frame(width: .infinity)
Spacer()
}.padding()
Button(action: {}) {
Text("Test Button")
.foregroundColor(.white)
.font(.headline)
.frame(width: .infinity)
Spacer()
}.padding()
Button(action: {}) {
Text("Test Button")
.foregroundColor(.white)
.font(.headline)
.frame(width: .infinity)
Spacer()
}.padding()
}
}.frame(width: 250, height: 100)
}
Spacer()
}
}
}
Change your .frame(width: 250, height: 100) to .frame(width: 250, height: 100, alignment: .top) Your original example defaults to .center, which is why it expands into the red rectangle.