Search results for

swiftui

16,633 results found

Post

Replies

Boosts

Views

Activity

SwiftUI and unit testing
Appologies for cross-posting but I just found the right forum to ask this question.I am trying to write unit tests for SwiftUI views but finding zero resources on the web for how to go about that. Any pointers?I have a view like the followingstruct Page: View { @EnvironmentObject var service: Service var body: some View { NavigationView { ScrollView(.vertical) { VStack { Text(Some text)) .font(.body) .navigationBarTitle(Text(Title))) Spacer(minLength: 100) } } } } }I started writing a test like thisfunc testPage() { let page = Page().environmentObject(Service()) let body = page.body XCTAssertNotNil(body, Did not find body) }But then how do I get the views inside the body? How do I test their properties?As a matter of fact even this doesn't work. I am getting the following runtime exceptionThread 1: Fatal error: body() should not be called on ModifiedContent<Page,_EnvironmentKeyWritingModifier<Optional<Service>>>.
1
0
5.1k
Oct ’19
SwiftUI List performane
I have a performance issue with List when I have a large amount of data that is replaced. Given the code below, a data set of about 3500 name items are loaded from a fetch request. Depending of the selected gender in the segmented picker these items are filtered and the displayed in the List. When the List first render I have no performance issue with the loading and rendering of items. It scrolls nicely and smoothly through 1700 items. But as soon as I switch gender through the segemented picker it takes about 30-45 seconds to render the List again.I think this has to do with removing 1700 items and the inserting 1500 items again from the List. Is there a best practice how to reload a large amount of items in SwiftUI? Or can I reset the List before I load it again, since there is no issue initially.Anyone else having issue same issue?struct NameList: View { @ObservedObject fileprivate var global = GlobalSettings() @FetchRequest( entity: Name.entity(), sortDescriptors: [NSSortDescriptor(key: name, as
17
0
15k
Oct ’19
SwiftUI ForEach with id fails at Runtime on Beta 6.1+
The following simplified code compiles but fails at runtime (crash!) precondition failure: attribute failed to set an initial value: X error.Doing some tests, it seems that SwiftUI ForEach does not like the id:.self as would be the common way of using a simple Array on ForEach?I know that it is easy and convenient to comply the content of Array to identifiable but there are cases (such as using CaseIterable) where just using id:.self would be the way to go:@State var Value: Float = 0.0 private var baseChoices: [Float] = [436, 437, 438, 439, 440, 441, 442, 443, 444] // declared at the beginning of view Struct /// inside the body: HStack { Picker(selection: $Value, label: Text(watchos.tuner.base).bold().modifier(PickerLabel()) ) { ForEach(baseChoices, id: .self) { val in Text((Int(val))).minimumScaleFactor(0.5) } } }Note that this works totally fine on WatchOS 6 and any non-beta but systematically leading to Crash on all available betas as of today.This seems to be the case with all uses of id on ForEa
1
0
2.3k
Oct ’19
Reply to Background Color of Navigation View with List View
Thanks for Paul Hudson (hackingwithswift.com), I got the answer for this today. In the init, changing the tableview color takes a UIColor. The .listRowBackground uses the SwiftUI Color. If you set each of those with rgb, they will match. If you use just .green, they will be different.struct ContentView: View { init() { UITableView.appearance().backgroundColor = .green // Uses UIColor } var body: some View { NavigationView { List { ForEach(1...3, id: .self) { index in NavigationLink( destination: DetailView()) { ContentCell() } .frame(height: 100) .listRowBackground(Color.blue) // Uses Color } } .navigationBarTitle(My List) } //.background(Color.green)// Not working } } struct ContentCell: View { var body: some View { GeometryReader { geometry in VStack { Text(An item to display.) } .frame(width: (geometry.size.width), height: geometry.size.height, alignment: .center) .background(Color.red)// Working } } } struct DetailView: View { var body: some View { VStack { Text (At the detail view) } .frame(minW
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’19
SwiftUI view gets wider than host/parent view?
I created a UIHostingController to put a SwiftUI view in my app. The root view contains a VStack, and inside that some HStacks and Pickers. The picker pushes the view wider than the screen. I can see in Xcode (Debug View Hierarchy) that my screen is 320 (iPhone SE) and my SwiftUI View is 328. Usually I would fix this with auto layout constraints pinning left and right edges to parent with priority 1000. What is the equivalent in SwiftUI?Rob
3
0
4.5k
Oct ’19
Why is my child SwiftUI view not updating?
I have a simple child view that contains a text view.Changing the state of my parent view should change that of my child, but it does not happen.See the example code below. The Text (green) in ContentView is updated correctly when the button is pressed, but not the child view (Orange).Why?struct ContentView: View { @State var msg: String = var body: some View { VStack { Button(action: { self.msg = Hallo World }, label: { Text(Say Hallo)}) ChildView(msg: msg).padding().background(Color.orange) Text(msg).padding().background(Color.green) }.frame(maxWidth: .infinity, maxHeight: .infinity) } } struct ChildView: View { @State var msg: String var body: some View { Text(msg) } }
4
0
21k
Oct ’19
How can create a share sheet/view with SwiftUI for iOS 13 ?
How can create a new Share View ( now called Share sheet ? ) for iOS 13 ?In the new features of the iOS 13, we saw how the look (and possibly the way) with which we can share with other applications has changed.As I read in this article : iOS 13: The Ars Technica reviewI want to be able to display this menu with all the share options available in my SwiftUI app:So, even easier, how can I simply share a link - a text - to another App with Swift 5 using SwiftUI?What is the new way proposed ? What is the code that I have to write in my View to display this menu of options?Thank you.
8
0
23k
Oct ’19
Reply to How can create a share sheet/view with SwiftUI for iOS 13 ?
It works in much the same way as in UIKit, but you'll have to use .sheet(isPresented:content:) to make the view appear.What you need is the UIActivityViewController, which means you'll have to create a SwiftUI UIViewControllerRepresentable view for that. In this case it's fairly straightforward, since you're not modifying the content or maintaining any state, you're effectively wrapping the initializer. This version works for me:struct ShareSheet: UIViewControllerRepresentable { typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void let activityItems: [Any] let applicationActivities: [UIActivity]? = nil let excludedActivityTypes: [UIActivity.ActivityType]? = nil let callback: Callback? = nil func makeUIViewController(context: Context) -> UIActivityViewController { let controller = UIActivityViewController( activityItems: activityItems, applicationActivities: applicationActivities) controller.excludedActivityTypes = ex
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’19
Reply to Consideration to make SwiftUI a library independent to the system version like Swift before?
SwiftUI is a standalone library now. It lives in /System/Library/Frameworks/SwiftUI.framework, and is dynamically linked like any other framework, with the stable Swift v5 ABI.Or do you mean that it would be available in some format that you can include in your application bundle as a dependency? That's highly unlikely, given that it's very dependent on particular versions of the operating system's other libraries; there's going to be some special sauce inside UIKit, and SwiftUI also links directly against CoreUI, which is the private framework that handles 'theme' rendering, i.e. the look & feel of buttons, bars, etc. on each platform.Rest assured, though, that the pace of improvement will be brisk. Of all the teams at Apple, that one has some of the absolute best & brightest working on it. There are some rough edges right now, but most can be circumvented in one way or another, plus you can adopt it piecemeal via UIHostingController and friends—implement a modal controller's conten
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’19
How to set height and width in proportion with superview in SwiftUI?
I want to know that is it possible to adjust height and width in proportion according to superview in SwiftUI to any UIControl ? As we are doing like : let txtFld = UITextField() txtFld.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: self.view.frame.width/2, height: self.view.frame.height*0.1)) I know we can somehow achieve to adjust width by using Spacer(), .padding and edgeInsets. But what about height? I have seen lots of tutorial for SwiftUI, everywhere height is static. Even on Apple's site. SwiftUI TutorialsIf anyone have knowledge about how to set height in proportion with view's height, please share here. Because at some point we need like that. For ex. If it needs to make button according to device height. (Let's say in iPhone SE = 40, iPhone 8P = 55, iPhone X = 65)
1
0
6.5k
Oct ’19
Reply to SwiftUI List performane
I have the exact same problem. It seems that SwiftUI is not very smart, it compares the entire list, not only the visible rows. And what is worst, it processes each row, so if you have complex rows showing more data from relationships, is even slower. I decided NOT doing long list with SwiftUI for now, but now I have another problems because my app uses a synchronizer for updating the coredata and when something is modified in the background, does not trigger the UI update, not even using @FetchRequest SwiftUI is still BETA IMHO
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’19