I have project in SwiftUI 2.0 and I want to update it for Swift 3.0, is it possible to do that?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have searchable properties in my project, everythings work, but it is looking bad when I search any items, it show "cancel" title inside of the search bar, how can I hide it?
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
I want to use onboarding screen in my project, and it is work but I want to use it just once time for app, I do not know how I will do it, is there any way?
struct ContentView: View {
@State private var onboardinDone = false
var data = OnboardingData.data
var body: some View {
Group {
if !onboardinDone {
OnboardingView(data: data, doneFunction: {
print("done onboarding")
})
} else {
MainScreen()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I have a simple app in SwiftUI, and I try to delete all list items with context menu , when I click context menu button, I want to remove all items, is it possible?
struct MyView: View {
@State private var selectedUsers: MyModel?
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
} .onDelete { (indexSet) in
selectedUsers.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
model:
struct MyModel: Identifiable, Hashable, Codable {
var id = UUID().uuidString
var name: String
}
var datas = [
MyModel(name: "david"),
MyModel(name: "marry"),
]
I have list items in SwiftUI, and when I delete list items I want to delete after alert menu, like
"do want to delete your list items, ""yes" or "no"
is it possible?
struct MyView: View {
@State private var selectedUsers: MyModel?
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
} .onDelete { (indexSet) in
self.datas.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
I have small SwiftUI app, and it throw an error like "Cannot find 'state' in scope" for this line
Register(state: state)
I guess it must be like that, but it is throw an error, I do not know what I missed? Any idea?
struct Register: View {
@ObservedObject private var viewModel: RegisterViewModel
init(state: AppState) {
self.viewModel =RegisterViewModel(authAPI: AuthService(), state: state)
}
var body: some View {
}
}
struct Register_Previews: PreviewProvider {
@ObservedObject private var viewModel:
RegisterViewModel
@State var pushActive = false
init(state: AppState) {
self.viewModel = RegisterViewModel(authAPI: AuthService(), state: state)
}
static var previews: some View {
Register(state: state)
}
}
class RegisterViewModel: ObservableObject {
@Published var state: AppState
init(authAPI: AuthAPI, state: AppState) {
self.authAPI = authAPI
self.state = state
}
}
}
I have simple app in SwiftUI, when I use @Binding var show : Bool for second register screen,
it is throw an error for this line of code
init(state: AppState) {
self.viewModel = RegisterViewModel(authAPI: AuthService(), state: state)
}
as a "Return from initializer without initializing all stored properties" any idea?
first screen:
import SwiftUI
struct RegisterFirstScreen: View {
@Binding var show : Bool
init(state: AppState) {
self.viewModel = RegisterViewModel(authAPI: AuthService(), state: state)
}
var body: some View {
NavigationLink(destination: RegisterSecondScreen(state: viewModel.state, show: $show),
isActive: self.$pushActive) {
Button {
viewModel.signUp()
} label: {
Text("Register Now")
.padding()
}
}
}
second screen:
struct RegisterSecondScreen: View {
@ObservedObject var state: AppState
@Binding var show : Bool
var body: some View {
Text("Next main screen")
}}
When I update my macbook to macOS13 Venture , I have a problem in top right corner, always give alots of notifications , how can I solve it?
I have simple project in swiftUI, everythings is work, but
HStack for Rectangle() as I mention below, do not alignment the Rectangle and text at the same line, and idea will be appreciated.
struct App: View {
var body: some View {
GeometryReader{g in
ZStack {
ForEach(0..data.count) { i in
DrawShape(center: CGPoint(x:g.frame(in: .global).width/2, y: g.frame(in: .global).height/2), index: i)
}
}
}
.frame(height: 200)
.clipShape(Circle())
.shadow(radius: 10)
VStack{
ForEach(data) { i in
HStack {
Text(i.name)
.frame(width:100)
.padding()
GeometryReader { g in
HStack{
Spacer(minLength: 0)
Rectangle()
.fill(i.color)
.frame(width: self.getWidth(width: g.frame(in: .global).width, value: i.percent) ,height: 10)
Text(String(format: "\(i.percent)", "%.0f"))
.fontWeight(.bold)
.padding(.leading,10)
.frame(width:80)
Spacer()
}.frame(width: 240, height: 30)
}
}
}
.padding()
Spacer()
}
}
func getWidth(width: CGFloat, value: CGFloat) - CGFloat {
let temp = value / 100
return temp * width
}
}
I have number.name array like 1.go, 2.java, 3.swift etc..and I do not want to edit the number again, user will only change name. Is it possible?
if words.count == 24 {
for (index, textField) in textFields.enumerated() {
textField.delegate = self
textField.firstDesign()
textField.text = "\(index + 1). \(words[index])"
mneArr.append(words[index])
}
I have data in row like Text(data.user) and I want to use searchable for it, I can use search bar, bat I want to use filter for user.
Text(data.user)
.searchable(text: $searchText, placement: .navigationBarDrawer)
like here, tere is some example for filter, but how can I use it for my data. Any idea?
.onChange(of: searchText) { searchText in
if !searchText.isEmpty {
articles = sampleArticles.filter { $0.title.contains(searchText) }
} else {
articles = sampleArticles
}
}
I have a simple app in SwiftUI, and I try to delete list items in app , project is working, but still list items not able to delete, I do not know what I did not put in my codes, any idea will be appreciated.
struct MyView: View {
@State private var selectedUsers: MyModel?
var body: some View {
ScrollView(.vertical, showsIndicators: false, content: {
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
self.delete(item: data)
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
} .onDelete { (indexSet) in
selectedUsers.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
model:
struct MyModel: Identifiable, Hashable, Codable {
var id = UUID().uuidString
var name: String
}
var datas = [
MyModel(name: "david"),
MyModel(name: "marry"),
]
I have multiple alert dialog in project, just one of them is work, but I am still do not know why I am not use second one, so how we can use multiple .alert dialog in SwiftUI?
struct MyView: View {
@State private var selectedUsers: MyModel?
@State var datas: [MyModel]
@State private var deleteRow = false
@State private var deleteRows = false
var body: some View {
ScrollView(.vertical, showsIndicators: false, content:
{
VStack(content: {
ForEach(datas){ data in
MyRowView(data: data)
.contextMenu {
Button(action: {
deleteRow = true
}) {
Text("delete")
}
Button(action: {
deleteRows = true
}) {
Text("delete")
}
}
.onTapGesture {
selectedUsers = data
}
.alert(isPresented: $deleteRow) {
Alert(title: Text("title"),
message: Text("message"),
primaryButton: .destructive(Text("Delete")) {
self.delete(item: data)
},
secondaryButton: .cancel())
}
.alert(isPresented: $deleteRows) {
Alert(title: Text("title"),
message: Text("message"),
primaryButton: .destructive(Text("Delete")) {
self.datas.removeAll()
},
secondaryButton: .cancel())
}
} .onDelete { (indexSet) in
self.datas.remove(atOffsets: indexSet)
}})
})}
private func delete(item data: MyModel) {
if let index = datas.firstIndex(where: { $0.id == data.id }) {
datas.remove(at: index)
}
}}
I want to use searchbar for name, but it is throw an arror like " Value of type 'Text' has no member 'searchable'" any idea?
@State private var searchText = ""
Text(data.name)
.font(.custom("Helvetica Neue", size: 14))
.searchable(text: $searchText)
I have TabView in ContentView and I want to add TabView for OnboardingView in OtherView, every things work, but it is throw error for TabView in OtherView like "Trailing closure passed to parameter of type 'Int' that does not accept a closure" I do not know why? Any idea?
ContentView:
struct TabView : View {
var body: some View{
VStack(spacing: 0){
.......
}
OtherView:
VStack {
TabView {
ForEach(onboardingData) { onboardingItem in
OnboardingCard(onboardingItem: onboardingItem)
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))
.indexViewStyle(PageIndexViewStyle (backgroundDisplayMode:
.always))
.foregroundColor(.white)
}