I am using a wheel picker in SwiftUI, with a custom larger font, and there is virtually no spacing between selections. How can i add spacing in between each selection?
Picker("", selection: $dqc.amountOfRandomPerDay) {
Text("Two times")
.font(.poppinsLight(size: 30))
.tag(2)
Text("Three times")
.font(.poppinsLight(size: 30))
.padding(10)
.frame(height: 100)
.tag(3)
Text("One time")
.font(.poppinsLight(size: 30))
.tag(1)
}
.pickerStyle(.wheel)
For that you have to use .scaleEffect(1.5)
Picker("", selection: $dqc.amountOfRandomPerDay) {
Text("Two times").tag(2)
Text("Three times").tag(3)
Text("One time").tag(1)
}
.pickerStyle(.wheel)
.scaleEffect(1.5) // Change the number according size which you required
Related
This is what I have in code:
VStack(spacing: 2) {
Text("23:11:45") or "23:11"
.foregroundColor(Color(uiColor: mode.underlayBackgroundColor))
.font(.liberationMonoRegular(withSize: 46))
.multilineTextAlignment(.center)
.scaledToFill()
.minimumScaleFactor(0.5)
ProgressView(value: progress, total: 1)
.tint(Color(uiColor: mode.underlayBackgroundColor))
.foregroundColor(Color(uiColor: mode.underlayBackgroundColor)
.opacity(0.3))
}
.fixedSize()
.background(.blue)
VStack is wrapped into:
ScrollView {
}
.background(.red)
.frame(maxWidth: .infinity)
here is version for "23:11:45"
here is version for "23:11"
As you can see the Text with 23:11:45 String extends VStack although it should change the scale of the font. What to do to make it working? Maximum width is 100% - padding for both sides.
Your use of fixedSize propagates down and tells the Text not to scale down.
Compare:
Here's the code that generated that image:
VStack(spacing: 10) {
Text("with fixedSize:")
VStack {
Text("Hello world")
Text("Hello world")
.font(Font.custom("Palatino", fixedSize: 46))
}
.fixedSize()
.minimumScaleFactor(0.5)
.frame(width: 50)
.background(Color.black.opacity(0.2))
Spacer().frame(height: 40)
Text("without fixedSize:")
VStack {
Text("Hello world")
Text("Hello world")
.font(Font.custom("Palatino", fixedSize: 46))
}
.minimumScaleFactor(0.5)
.frame(width: 50)
.background(Color.black.opacity(0.2))
}
.lineLimit(1)
It doesn't matter if the fixedSize comes before or after the minimumScaleFactor. It does matter if it's before or after the frame.
I have a login screen in which I want to cover VStack whole white space which is remaining. I have 2 Vstack If I give Spacer to second one its not working.
My code
struct loginView: View {
#State private var stringOfTextField: String = String()
var body: some View {
ZStack {
LinearGradient(colors: [Color("primarycolor"), Color("secondarycolor")],
startPoint: .top,
endPoint: .center).ignoresSafeArea()
VStack() {
Text("Login").foregroundColor(.white).font(
.system(size: 18)
)
Image("logo")
Spacer()
}
VStack{
TextField("Enter Email", text: $stringOfTextField)
.padding()
.overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.gray, style: StrokeStyle(lineWidth: 1.0)))
.padding(.bottom)
TextField("Enter Password", text: $stringOfTextField)
.padding()
.overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.gray, style: StrokeStyle(lineWidth: 1.0)))
.padding(.bottom)
Text("Forgot Password ?")
.frame(maxWidth: .infinity, alignment: .trailing)
.padding(.bottom)
Button(action: {
print("sign up bin tapped")
}) {
Text("SIGN IN")
.frame(minWidth: 0, maxWidth: .infinity)
.font(.system(size: 18))
.padding()
.foregroundColor(.white)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color("secondarycolor"), lineWidth: 1)
)
}
.background(Color("secondarycolor"))
.cornerRadius(25)
}
.padding(.vertical, 60)
.padding(.horizontal, 20)
.background(Color.white)
.cornerRadius(30)
}
}
}
I try to add spacer in Vstack But its not working maybe due to Zstack. Its going on top if I give Spacer() to VStack
I think I understood what you are trying to achieve, just please next time post a complete minimal reproducible example, replacing the custom colours and images.
You can keep the logo visible and cover the bottom area with 4 changes:
Wrap your current VStack with a new one; you need this to add a Spacer() that will keep the logo visible.
Add a Spacer() at the top of the new VStack, right before the existing one
Add a Spacer() at the bottom of the existing VStack
Add the .ignoreSafeArea() modifier to the ZStack
Here's the code:
ZStack {
LinearGradient(colors: [.primary, .secondary],
startPoint: .top,
endPoint: .center).ignoresSafeArea()
VStack() {
Text("Login").foregroundColor(.white).font(
// ... rest of the code
}
// 1) New VStack around the first one
VStack {
// 2) Spacer() before the existing VStack with minimum length, to keep the logo visible
Spacer(minLength: 200)
VStack{
TextField("Enter Email", text: $stringOfTextField)
// ... rest of the code
.cornerRadius(25)
// 3) New Spacer() at the bottom of the existing VStack
Spacer()
}
.padding(.vertical, 60)
.padding(.horizontal, 20)
.background(Color.white)
.cornerRadius(30)
}
}
// 4) Add this modifier to the ZStack
.ignoresSafeArea(edges: [.bottom])
I have a search bar that I use within the Navigation Bar of a Navigation View, but I can only type one character into it at a time before it loses focus and I have to re tap on the text field to enter another character.
The Text Field is currently a toolbar item of the Navigation Bar:
.toolbar{
//The big blob of code is just to format the search bar
ToolbarItem(placement: .principal){
TextField("Search for Receipts", text: $searchInput, onCommit: {self.runSearch = true})
.padding(7)
.padding(.horizontal, 50)
.background(Color(.systemGray6))
.cornerRadius(8)
.overlay(
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding(.leading, 8)
if isEditing {
Button(action: {
self.searchInput = ""
}) {
Image(systemName: "multiply.circle.fill")
.foregroundColor(.gray)
.padding(.trailing, 8)
}
}
}
)
.onTapGesture {
self.isEditing = true
}
.overlay(
RoundedRectangle(cornerRadius: 8.0)
.stroke(lineWidth: 1.0)
//.padding(.horizontal, 10)
.foregroundColor(.gray)
)
}
The isEditing variable is used to include a grey cancellation button that lets the user clear their input.
Does anyone know of a way to make it act like a normal text field, or the reason why this is happening?
Is there same thing as ImageEdgeInsets for SwiftUI. For example I have button 60x60. And want to make inset for image inside.
VStack(alignment: .leading, spacing: 0.0){
Section{
Button(action: {
if self.accountViewModel.signInContext == .quote{
self.presentationMode.wrappedValue.dismiss()
}else{
self.viewController?.dismiss(animated: true)
}
}){
Image("Close Icon").renderingMode(.template).padding(.vertical, 20).padding(.horizontal, 20).foregroundColor(.white).scaledToFit()
}
.frame(width: 60, height: 60)
Rectangle().line().padding(.horizontal, -30)
}
}
Yes, .padding is for that purpose, just needed to make image resizable. I suppose you need the following
Image("Close Icon")
.renderingMode(.template)
.resizable()
.aspectRatio(contentMode: .fit)
.padding(.vertical, 20).padding(.horizontal, 20)
I have a login screen and wish to navigate to a new screen when the Login button is clicked.
I did try to wrap the button and the entire screen layout under NavigationView and embedded the button in a Navigation Link.
I am unable to figure out how to show the new screen when the button is clicked. Following is the code for the login screen.
ZStack {
Color.red
.edgesIgnoringSafeArea(.all)
VStack(alignment: .center, spacing: 180.0) {
Text("SwiftUI")
.font(.largeTitle)
.bold()
.padding()
VStack(alignment: .center, spacing: 25) {
TextField("Username", text: $userName)
.padding(.all)
.background(Color.white)
.cornerRadius(10)
TextField("Password", text: $userPassword)
.padding(.all)
.background(Color.white)
.cornerRadius(10)
Toggle(isOn: $isFirstTimeUser) {
Text("First Time User")
.font(.headline)
.bold()
.padding(.horizontal, -10)
.foregroundColor(Color.white)
}.padding(.horizontal, 17)
Button(action: {
if self.userName.count <= 5 {
self.isAlertShown = true
} else {
}
})
{
Text(isFirstTimeUser ? "SignUp" : "Login")
.fontWeight(.medium)
.font(.title)
.foregroundColor(Color.red)
.padding(.horizontal, 10)
}.padding()
.background(Color.white)
.cornerRadius(10)
.alert(isPresented: $isAlertShown) {
() -> Alert in
Alert(title: Text("UserName Invalid"), message: Text("Username has to be more than 5 characters"), dismissButton:.default(Text("Got that!")))
}
}.padding(.horizontal, 17)
}
}
Here is possible approach
1) Add link tag state variable to your View
#State private var current: Int? = nil
2) Wrap your view hierarchy into NavigationView to make possible NavigationLink to work
3) Add tagged NavigationLink above your button
NavigationLink(destination: YourDestinationViewHere(), tag: 1, selection: $current) {
EmptyView()
}
4) Add link tag selection to button action
Button(action: {
if self.userName.count <= 5 {
self.isAlertShown = true
} else {
self.current = 1 // this activates NavigationLink with specified tag
}
})