expected result
current result
I want to make it animation controllable
Open - text shows with animation
Close - text hide with animation
My code:
struct HomeView: View {
#State var isOpen = false
var body: some View {
ZStack {
Image("background")
.resizable()
VStack(alignment: .leading) {
Text("Appearing title text")
.frame(width: isOpen ? 200 : 0, height: 20, alignment: .leading)
.animation(.linear(duration: 2))
.font(.system(size: 20))
.foregroundColor(.white)
HStack {
Button("Open") {
isOpen = true
}
Button("Close") {
isOpen = false
}
}
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
.padding(.top, 100)
}
.ignoresSafeArea()
}
}
Prompt me, please, how can I do it?
Is there a way to enable Gradient Buttons [ + | – ] with SwiftUI? Can't find any useful information about this topic.
https://developer.apple.com/design/human-interface-guidelines/components/menus-and-actions/buttons#gradient-buttons/
Update
I made a few cosmetic changes to #workingdogsupportUkraine answer. Now Gradient Buttons looks similar as in the Settings app on macOS 13. Please feel free to suggest any enhancements
import SwiftUI
struct TestView: View {
#State private var selection: Int?
struct GradientButton: View {
var glyph: String
var body: some View {
ZStack {
Image(systemName: glyph)
.fontWeight(.medium)
Color.clear
.frame(width: 24, height: 24)
}
}
}
var body: some View {
Form {
Section {
List(selection: $selection) {
ForEach(0 ..< 5) { Text("Item \($0)") }
}
.padding(.bottom, 24)
.overlay(alignment: .bottom, content: {
VStack(alignment: .leading, spacing: 0) {
Divider()
HStack(spacing: 0) {
Button(action: {}) {
GradientButton(glyph: "plus")
}
Divider().frame(height: 16)
Button(action: {}) {
GradientButton(glyph: "minus")
}
.disabled(selection == nil ? true : false)
}
.buttonStyle(.borderless)
}
.background(Rectangle().opacity(0.04))
})
}
}
.formStyle(.grouped)
}
}
you could try something simple like this:
struct ContentView: View {
var body: some View {
List {
ForEach(0 ..< 5) { Text("item \($0)") }
HStack {
Button(action: {print("plus")}) {
Image(systemName: "plus").imageScale(.small)
}
Divider()
Button(action: {print("minus")}) {
Image(systemName: "minus").imageScale(.small)
}
}.buttonStyle(.borderless)
.frame(width: 66, height: 33)
}
}
}
I have tried to implement a search bar within my app (using swiftUI), which shows up when I build the app. However, it won't allow me to type into the actual text field. My code is below. I have been searching for solutions to this problem for awhile and can't seem to see any problems in my code. Is there something wrong with my TextField?
Code for search bar -
import SwiftUI
struct SearchBar: View {
#Binding var text: String
#State private var isEditing = false
var body: some View {
HStack {
TextField("Search...", text: $text)
// .foregroundColor(Color("Teal"))
// .background(Color("Grey"))
.overlay(
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(Color(UIColor.systemGray3))
.frame(minWidth:0, maxWidth: .infinity, alignment: .leading)
.padding(EdgeInsets.init(top: 0, leading: 30, bottom: 0, trailing: 20))
// Search icon
if isEditing {
Button(action: {
self.text = ""
}, label: {
Image(systemName: "multiply.circle")
.foregroundColor(Color(UIColor.systemGray3))
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 30))
// Delete button
})
}
}
).onTapGesture {
self.isEditing = true
}
if isEditing{
Button(action: {
self.isEditing = false
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
// Hide delete button when search bar is not selected
}){
}
}
}
}
}
Code implementing search bar into my view -
import SwiftUI
struct BusinessList: View {
#EnvironmentObject var modelData: ModelData
#State private var showFavoritesOnly = false
#State var searchText = ""
var filteredBusinesses: [Business] {
modelData.businesses.filter { business in
(!showFavoritesOnly || business.isFavorite)
}
}
var body: some View {
NavigationView {
ScrollView {
LazyVStack {
VStack {
SearchBar(text: $searchText)
.padding(.bottom)
.padding(.top)
.padding(.top)
.padding(.top)
.padding(.top)
// Search bar won't show text when 'typed into'
ScrollView(.horizontal, showsIndicators: false) {
HStack {
Button(action: {
showFavoritesOnly = true
}) {
Text("Favourites")
.font(.caption)
.fontWeight(.medium)
.foregroundColor(Color.white)
.frame(width: 100.0, height: 30)
.background(Color("Teal"))
// How the button looks
.cornerRadius(25)
.zIndex(1)
.textCase(.uppercase)
.padding(EdgeInsets.init(top: 0, leading: 20, bottom: 0, trailing: 0))
}
Button(action: {
}) {
Text("Hospitality")
.font(.caption)
.fontWeight(.medium)
.foregroundColor(Color.white)
.frame(width: 100.0, height: 30)
.background(Color("Teal"))
// How the button looks
.cornerRadius(25)
.zIndex(1)
.textCase(.uppercase)
}
Button(action: {
}) {
Text("Retail")
.font(.caption)
.fontWeight(.medium)
.foregroundColor(Color.white)
.frame(width: 100.0, height: 30)
.background(Color("Teal"))
// How the button looks
.cornerRadius(25)
.zIndex(1)
.textCase(.uppercase)
}
Button(action: {
}) {
Text("Lifestyle")
.font(.caption)
.fontWeight(.medium)
.foregroundColor(Color.white)
.frame(width: 110.0, height: 30)
.background(Color("Teal"))
// How the button looks
.cornerRadius(25)
.zIndex(1)
.textCase(.uppercase)
}
}
.padding(.bottom)
}
}
.background(Color(UIColor.white))
.shadow(radius: 6)
.padding(.bottom)
ForEach(filteredBusinesses) { business in
NavigationLink(destination: BusinessDetail(business: business)) {
BusinessRow(business: business)
}
}
}
}
.navigationBarHidden(true)
.ignoresSafeArea()
}
}
}
struct BusinessList_Previews: PreviewProvider {
static var previews: some View {
BusinessList()
}
}
your code works for me with minor changes to the colors, nothing major. Maybe the colors did not show your typing, but the text was there.
This is the test I did:
import SwiftUI
#main
struct TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct SearchBar: View {
#Binding var text: String
#State private var isEditing = false
var body: some View {
HStack {
TextField("Search...", text: $text)
// .foregroundColor(Color("Teal"))
// .background(Color("Grey"))
.overlay(
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
.frame(minWidth:0, maxWidth: .infinity, alignment: .leading)
.padding(EdgeInsets.init(top: 0, leading: 30, bottom: 0, trailing: 20))
// Search icon
if isEditing {
Button(action: {
self.text = ""
}, label: {
Image(systemName: "multiply.circle")
.foregroundColor(.gray)
.padding(EdgeInsets.init(top: 0, leading: 0, bottom: 0, trailing: 30))
// Delete button
})
}
}
).onTapGesture {
self.isEditing = true
}
if isEditing{
Button(action: {
self.isEditing = false
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
// Hide delete button when search bar is not selected
}){
}
}
}
}
}
struct ContentView: View {
#State var searchText = ""
var body: some View {
SearchBar(text: $searchText)
.frame(width: 222, height: 55).padding(.horizontal, 20)
.border(.red)
}
}
EDIT:
Your new code works for me, when you remove
.shadow(radius: 6)
and
.ignoresSafeArea()
EDIT: Explanation to the problem
This code is producing an error where the poster doesn't know how to solve it.
This is a new edit of the previous post.
I received the error on >Geometry Reader. This post includes all of the code. This new post includes the Sign-Up and Login code as requested. I hope that it is in a readable format. I made some corrections that I hope will help. The code is listed below:
import SwiftUI
struct ContentView: View {
var body: some View {
Home()
// for light status bar...
.preferredColorScheme(.dark)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Home : View {
#State var index = 0
var body: some View{
GeometryReader{ _ in // ( ERROR MESSAGE OCCURS HERE)--> ("Type '()' cannot conform to'View'; only struct/enum/class types can conform to protocols" and "Required by generic struct 'GeometryReader' where 'Content' = '()'")
VStack{
Image("logo")
.resizable()
.frame(width:60, height: 60)
ZStack{
SignUp(index: self.$index)
*// changing view order...*
.zIndex(Double(self.index))
Login(index: self.$index)
}
HStack(spacing: 15){
Rectangle()
.fill(Color("Blue"))
.frame(height: 1)
Text("OR")
Rectangle()
.fill(Color("Blue"))
.frame(height: 1)
}
.padding(.horizontal, 20)
.padding(.top, 50)
*// because login button is moved 25 in y axis and 25 padding = 50*
.background(Color("Orange").edgesIgnoringSafeArea(.all))
//Curve...
HStack(spacing: 25){
Button(action: {
}) {
Image("Unknown")
.resizable()
.renderingMode(.original)
.frame(width: 50, height: 50)
.clipShape(Circle())
}
Button(action: {
}) {
Image("fb")
.resizable()
.renderingMode(.original)
.frame(width: 50, height: 50)
.clipShape(Circle())
}
Button(action: {
}) {
Image("instagram")
.resizable()
.renderingMode(.original)
.frame(width: 50, height: 50)
.clipShape(Circle())
}
}
.padding(.top, 30)
}
.padding(.vertical)
struct CShape: Shape {
func path(in rect: CGRect) -> Path {
return Path {path in
*//right side curve...*
path.move(to: CGPoint(x: rect.width, y: 100))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
path.addLine(to: CGPoint(x: 0, y: rect.height))
path.addLine(to: CGPoint(x: 0, y: 0))
}
}
}
struct CShape1: Shape {
func path(in rect: CGRect) -> Path {
return Path {path in
*//left side curve...*
path.move(to: CGPoint(x: 0, y: 100))
path.addLine(to: CGPoint(x: 0, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: 0))
}
}
}
struct Login : View {
#State var email = ""
#State var pass = ""
#Binding var index : Int
var body : some View {
ZStack(alignment: .bottom) {
VStack{
HStack{
VStack(spacing:10){
Text("Login")
.foregroundColor(self.index == 0 ? .white : .gray)
.font(.title)
.fontWeight(.bold)
Capsule()
.fill(self.index == 0 ? Color.blue : Color.clear)
.frame(width:100, height: 5)
}
Spacer(minLength:0)
}
.padding(.top, 30)// for top curve...
VStack{
HStack(spacing:15){
Image(systemName: "envelope")
.foregroundColor(Color("Blue"))
TextField("Email Adress", text: self.$email)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 40)
VStack{
HStack(spacing:15){
Image(systemName: "eye")
.foregroundColor(Color("Orange"))
SecureField("Password", text: self.$pass)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 30)
HStack{
Spacer(minLength: 0)
Button(action: {
}) {
Text("Forget Password?")
.foregroundColor(Color.white.opacity(0.6))
}
}
.padding(.horizontal)
.padding(.top, 30)
}
.padding()
// bottom padding...
.padding(.bottom, 65)
.background(Color("LightBlue"))
.clipShape(CShape())
.contentShape(CShape())
.shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
.onTapGesture{
self.index = 0
}
.cornerRadius(35)
.padding(.horizontal,20)
// Button...
Button(action: {
}) {
Text("LOGIN")
.foregroundColor(.white)
.fontWeight(.bold)
.padding(.vertical)
.padding(.horizontal, 50)
.background(Color("LightBlue"))
.clipShape(Capsule())
// shadow ...
.shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
}
// moving view down...
.offset(y: 25)
.opacity(self.index == 0 ? 1 : 0)
}
}
}
// SignUp Page...
struct SignUp : View {
#State var email = ""
#State var pass = ""
#State var Repass = ""
#Binding var index: Int
var body : some View {
ZStack(alignment: .bottom) {
VStack{
HStack{
Spacer(minLength:0)
VStack(spacing: 10){
Text("SignUp")
.foregroundColor(self.index == 1 ? .white : .gray)
.font(.title)
.fontWeight(.bold)
Capsule()
.fill(self.index == 1 ? Color.blue : Color.clear)
.frame(width:100, height: 5)
}
}
.padding(.top, 30)// for top curve...
VStack{
HStack(spacing:15){
Image(systemName: "envelope")
.foregroundColor(Color("Orange"))
TextField("Email Adress", text: self.$email)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 40)
VStack{
HStack(spacing:15){
Image(systemName: "eye")
.foregroundColor(Color("Orange"))
SecureField("Password", text: self.$pass)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 30)
// replacing forget password with reenter password...
// so same height will be maintained...
VStack{
HStack(spacing:15){
Image(systemName: "eye")
.foregroundColor(Color("Orange"))
SecureField("Password", text: self.$Repass)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 30)
}
.padding()
// bottom padding...
.padding(.bottom, 65)
.background(Color("Blue"))
.clipShape(CShape1())
//clipping the content shape also for tap gesture...
.contentShape(CShape1())
// shadow...
.shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
.onTapGesture {
self.index = 1
}
.cornerRadius(35)
.padding(.horizontal,20)
*// Button...*
Button(action: {
}) {
Text("SIGNUP")
.foregroundColor(.white)
.fontWeight(.bold)
.padding(.vertical)
.padding(.horizontal, 50)
.background(Color("Blue"))
.clipShape(Capsule())
*// shadow ...*
.shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
}
*// moving view down...*
.offset(y: 25)
*// hiding view when its in background...*
*// only button...*
.opacity(self.index == 1 ? 1 : 0)
}
}
}
}
}
}
So the problem in your code is that you are defining views inside a GeomtryReader and that's a big no no. So a fix would be to move the Login and Singup outside the GeomtryReader or even better and better practice is just create a new file for each view and add it's code in that file. For example one file for Login.swift and another for Register.swift and maybe another called Shapes which includes multiple shapes and exports them.
What you were doing is something similar to this
struct ContentView: View {
var body: some View {
GeomtryReader { _ in
Text("test")
// Here is where the bug would happen
struct NewView: View {
var body: some View {
Text("Second View")
}
}
//////////////////////////////////////
}
}
}
You can see if you copy and paste the above code it will generate the same error. What you should do is move NewView outside of GeomtryReader
Something like this
struct ContentView: View {
var body: some View {
return GeomtryReader { _ in
Text("test")
}
// This will fix the error
struct NewView: View {
var body: some View {
Text("Second View")
}
}
//////////////////////////////////////
}
}
Notice where I have moved the code. Also pay attention I have added Return to the GeomtryReader and that's because body is a computed property that expects to have a value of View but in this case we are confusing the compiler as to which View we want it to be the returned value so we have to manually specify it. If you don't want to include return then you would have to move NewView outside body or even better outside ContentView all together.
In any case here is your code working 100%, you can copy and paste it.
import SwiftUI
struct ContentView: View {
var body: some View {
Home()
// for light status bar...
.preferredColorScheme(.dark)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Home : View {
#State var index = 0
var body: some View{
return GeometryReader{ _ in // ( ERROR MESSAGE OCCURS HERE)--> ("Type '()' cannot conform to'View'; only struct/enum/class types can conform to protocols" and "Required by generic struct 'GeometryReader' where 'Content' = '()'")
VStack{
Image("logo")
.resizable()
.frame(width:60, height: 60)
ZStack{
SignUp(index: self.$index)
// changing view order...*
.zIndex(Double(self.index))
Login(index: self.$index)
}
HStack(spacing: 15){
Rectangle()
.fill(Color("Blue"))
.frame(height: 1)
Text("OR")
Rectangle()
.fill(Color("Blue"))
.frame(height: 1)
}
.padding(.horizontal, 20)
.padding(.top, 50)
// because login button is moved 25 in y axis and 25 padding = 50*
.background(Color("Orange").edgesIgnoringSafeArea(.all))
// Curve...
HStack(spacing: 25){
Button(action: {
}) {
Image("Unknown")
.resizable()
.renderingMode(.original)
.frame(width: 50, height: 50)
.clipShape(Circle())
}
Button(action: {
}) {
Image("fb")
.resizable()
.renderingMode(.original)
.frame(width: 50, height: 50)
.clipShape(Circle())
}
Button(action: {
}) {
Image("instagram")
.resizable()
.renderingMode(.original)
.frame(width: 50, height: 50)
.clipShape(Circle())
}
}
.padding(.top, 30)
}
.padding(.vertical)
}
struct Login : View {
#State var email = ""
#State var pass = ""
#Binding var index : Int
var body : some View {
ZStack(alignment: .bottom) {
VStack{
HStack{
VStack(spacing:10){
Text("Login")
.foregroundColor(self.index == 0 ? .white : .gray)
.font(.title)
.fontWeight(.bold)
Capsule()
.fill(self.index == 0 ? Color.blue : Color.clear)
.frame(width:100, height: 5)
}
Spacer(minLength:0)
}
.padding(.top, 30)// for top curve...
VStack{
HStack(spacing:15){
Image(systemName: "envelope")
.foregroundColor(Color("Blue"))
TextField("Email Adress", text: self.$email)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 40)
VStack{
HStack(spacing:15){
Image(systemName: "eye")
.foregroundColor(Color("Orange"))
SecureField("Password", text: self.$pass)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 30)
HStack{
Spacer(minLength: 0)
Button(action: {
}) {
Text("Forget Password?")
.foregroundColor(Color.white.opacity(0.6))
}
}
.padding(.horizontal)
.padding(.top, 30)
}
.padding()
// bottom padding...
.padding(.bottom, 65)
.background(Color("LightBlue"))
.clipShape(CShape())
.contentShape(CShape())
.shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
.onTapGesture{
self.index = 0
}
.cornerRadius(35)
.padding(.horizontal,20)
// Button...
Button(action: {
}) {
Text("LOGIN")
.foregroundColor(.white)
.fontWeight(.bold)
.padding(.vertical)
.padding(.horizontal, 50)
.background(Color("LightBlue"))
.clipShape(Capsule())
// shadow ...
.shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
}
// moving view down...
.offset(y: 25)
.opacity(self.index == 0 ? 1 : 0)
}
}
}
//SignUp Page...
struct SignUp : View {
#State var email = ""
#State var pass = ""
#State var Repass = ""
#Binding var index: Int
var body : some View {
ZStack(alignment: .bottom) {
VStack{
HStack{
Spacer(minLength:0)
VStack(spacing: 10){
Text("SignUp")
.foregroundColor(self.index == 1 ? .white : .gray)
.font(.title)
.fontWeight(.bold)
Capsule()
.fill(self.index == 1 ? Color.blue : Color.clear)
.frame(width:100, height: 5)
}
}
.padding(.top, 30)// for top curve...
VStack{
HStack(spacing:15){
Image(systemName: "envelope")
.foregroundColor(Color("Orange"))
TextField("Email Adress", text: self.$email)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 40)
VStack{
HStack(spacing:15){
Image(systemName: "eye")
.foregroundColor(Color("Orange"))
SecureField("Password", text: self.$pass)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 30)
// replacing forget password with reenter password...
// so same height will be maintained...
VStack{
HStack(spacing:15){
Image(systemName: "eye")
.foregroundColor(Color("Orange"))
SecureField("Password", text: self.$Repass)
}
Divider().background(Color.white.opacity(0.5))
}
.padding(.horizontal)
.padding(.top, 30)
}
.padding()
// bottom padding...
.padding(.bottom, 65)
.background(Color("Blue"))
.clipShape(CShape1())
//clipping the content shape also for tap gesture...
.contentShape(CShape1())
// shadow...
.shadow(color: Color.black.opacity(0.3), radius: 5, x: 0, y: -5)
.onTapGesture {
self.index = 1
}
.cornerRadius(35)
.padding(.horizontal,20)
// Button...*
Button(action: {
}) {
Text("SIGNUP")
.foregroundColor(.white)
.fontWeight(.bold)
.padding(.vertical)
.padding(.horizontal, 50)
.background(Color("Blue"))
.clipShape(Capsule())
// shadow ...*
.shadow(color: Color.white.opacity(0.1), radius: 5, x: 0, y: 5)
}
// moving view down...*
.offset(y: 25)
// hiding view when its in background...*
// only button...*
.opacity(self.index == 1 ? 1 : 0)
}
}
}
struct CShape: Shape {
func path(in rect: CGRect) -> Path {
return Path {path in
//right side curve...*
path.move(to: CGPoint(x: rect.width, y: 100))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
path.addLine(to: CGPoint(x: 0, y: rect.height))
path.addLine(to: CGPoint(x: 0, y: 0))
}
}
}
struct CShape1: Shape {
func path(in rect: CGRect) -> Path {
return Path {path in
//left side curve...*
path.move(to: CGPoint(x: 0, y: 100))
path.addLine(to: CGPoint(x: 0, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: 0))
}
}
}
}
}
I running SwiftUI with Xcode 11 Beat 5, and I wish to add an Alert to show the user when a Button is Tapped.
I am having problems though.
HStack{
Text("Inverted V")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(Color.black)
Spacer()
Button(action: {
}, label: {
Image(systemName: "questionmark.circle.fill")
.resizable()
.frame(width: 30, height: 30)
.foregroundColor(.red)
}).onTapGesture {
print("Hello Print")
}}
Can someone help with the code needed? Do I place it within the onTapGesture brackets?
I have a #State var showingAlert = false
Thank you in advance.
You would use something like the code below. If you are using onTapGesture you don't need a Button.
import SwiftUI
struct ContentView: View {
#State var showingAlert: Bool = false
var body: some View {
HStack{
Text("Inverted V")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(Color.black)
Spacer()
Image(systemName: "questionmark.circle.fill")
.resizable()
.frame(width: 30, height: 30)
.foregroundColor(.red)
.onTapGesture {
print("Hello Print")
self.showingAlert = true
}
}.alert(isPresented: $showingAlert, content: {
Alert(title: Text("Error"), message: Text("Error Reason"), dismissButton: .default(Text("OK")))
})
}
}