swiftui

What is the right way to present data from Realm in SwiftUI List

蓝咒 提交于 2021-02-05 07:00:34
问题 I’m trying to fetch all of the items from Realm and display them in a SwiftUI List but I keep getting an error. In a UIKit/Realm application, I would just create a Results variable to store all of the items from Realm then, I would fetch items in the viewDidLoad method and assign them to the variable. I'm trying to do the same thing in SwiftUI but I'm not sure how to structure my code, I keep getting an error saying that my Realm model should conform to the StringProtocol , I'm pretty sure

Placing a rectangle in front of ScrollView affects scrolling

て烟熏妆下的殇ゞ 提交于 2021-02-05 06:52:29
问题 I need to place a translucent rectangle on front of ScrollView but when i put everything (Rectangle & ScrollView) inside of a ZStack, scroll & touch events stop working within this rectangle. Atm I'm using .background modifier as it doesn't affect scrolling but I am still looking for way to make it work properly with rectangle placed over (in front of) my ScrollView . Is there any way to put a View over ScrollView so it wouldn't affect it's functionality? Here's the code i'm using now (i

Placing a rectangle in front of ScrollView affects scrolling

独自空忆成欢 提交于 2021-02-05 06:52:29
问题 I need to place a translucent rectangle on front of ScrollView but when i put everything (Rectangle & ScrollView) inside of a ZStack, scroll & touch events stop working within this rectangle. Atm I'm using .background modifier as it doesn't affect scrolling but I am still looking for way to make it work properly with rectangle placed over (in front of) my ScrollView . Is there any way to put a View over ScrollView so it wouldn't affect it's functionality? Here's the code i'm using now (i

Cross navigation with NavigationView

孤者浪人 提交于 2021-02-05 06:41:45
问题 I have the following structure enum Page { case chapter1 case chapter2 } struct ContentView: View { @State var page: Page? = nil var body: some View { NavigationView { VStack { NavigationLink(destination: Chapter1(page: self.$page), tag: .chapter1, selection: self.$page) { Text("Chapter 1") } NavigationLink(destination: Chapter2(page: self.$page), tag: .chapter2, selection: self.$page) { Text("Chapter 2") } } } } } struct Chapter1: View { @Binding var page: Page? var body: some View { VStack

Reference EnvironmentObject in ObservableObject

こ雲淡風輕ζ 提交于 2021-02-05 04:58:51
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

流过昼夜 提交于 2021-02-05 04:57:39
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

假装没事ソ 提交于 2021-02-05 04:57:38
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

Reference EnvironmentObject in ObservableObject

谁都会走 提交于 2021-02-05 04:56:25
问题 I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in: struct LoginView: View { @EnvironmentObject var userManager: UserManager var body: some View { Group { if userManager.isRegistered { ContentView() } else { RegisterView() } } } } ContentView have three ObservedObject properties, that uses combine to fetch content from a server with rest api's. struct ContentView: View { @EnvironmentObject var userManager: UserManager

How to center crop an image in SwiftUI

梦想的初衷 提交于 2021-02-05 03:30:29
问题 I'm new to SwiftUI. I guess everyone is at this point. I've been an app developer for about 6 years now and I feel stupid asking this question on StackOverflow. But I looked everywhere. How do I center crop an image in an ImageView in SwiftUI? I know there's an option to change the aspect ratio but I only see fit and fill. I just want the imageView to centerCrop(android term) the image. Does anybody know? 回答1: Android's ImageView.ScaleType documentation describes CENTER_CROP as: CENTER_CROP

How to specify the type information of an array that would hold SwiftUI custom views

徘徊边缘 提交于 2021-02-04 22:00:52
问题 I am learning SwiftUI and I do understand that to create a view element using SwiftUI framework, your custom type must conform to 'View' protocol. I have a requirement where I want to declare an array which would hold custom SwiftUI components that I would be creating by making a Struct implementing the 'View' protocol. Now I am not sure what to mention as the type of the array since, providing 'View' as the type is giving the compiler error as " Protocol 'View' can only be used as a generic