swiftui

SwiftUI and excessive redrawing

和自甴很熟 提交于 2021-01-28 09:50:55
问题 TL;DR : Applying visual effects to the contents of a ScrollView causes thousands of requests for the same (unchanging) image for each drag gesture. Can I reduce this? (In my real app, I have 50-odd images in the view, and the scrolling is correspondingly sluggish.) Gist To give a little life to a scrolling HStack of images, I applied a few transforms for a circular "carousel" effect. (Tips of the hat to sample code from John M. and Paul Hudson) The code is copy-paste-runnable as given. (You

SwiftUI and excessive redrawing

ぃ、小莉子 提交于 2021-01-28 09:50:07
问题 TL;DR : Applying visual effects to the contents of a ScrollView causes thousands of requests for the same (unchanging) image for each drag gesture. Can I reduce this? (In my real app, I have 50-odd images in the view, and the scrolling is correspondingly sluggish.) Gist To give a little life to a scrolling HStack of images, I applied a few transforms for a circular "carousel" effect. (Tips of the hat to sample code from John M. and Paul Hudson) The code is copy-paste-runnable as given. (You

SwiftUI: ForEach filters boolean

て烟熏妆下的殇ゞ 提交于 2021-01-28 09:34:05
问题 I'm trying desperately with ForEach to only display what has the value true. No matter what I try, false is also displayed. The air is out now and I think it's so simple that I just don't see it. Here is a piece of code to which it should apply: import SwiftUI struct txt: Identifiable { var id: Int var text: String var show: Bool } struct ContentView: View { @State private var array = [ txt(id: 000, text: "True", show: true), txt(id: 001, text: "True", show: true), txt(id: 002, text: "True",

How to dynamically size an image in SwiftUI for accessibility?

十年热恋 提交于 2021-01-28 08:54:11
问题 I have an image that I'm hard coding the size, but realized it's not scaling for larger size categories. How can I set a preferred size and let it scale up to different sizes automatically? This is what my code looks like: HStack(alignment: .top, spacing: 4) { Text("Some text") Button(action: { showAlert = true }) { Image(systemName: "questionmark.circle.fill") .resizable() .frame(width: 12, height: 12) .foregroundColor(.secondary) } } I also have other scenario where it's not using a SF

Using enumerated with ForEach in SwiftUI

时光总嘲笑我的痴心妄想 提交于 2021-01-28 08:52:11
问题 I want to know about syntax of using enumerated with ForEach . I am using a customID . Here is my code: ForEach(arrayNew.enumerated(), id:\.customID) { (index, item) in } Update: ForEach(Array(arrayNew.enumerated()), id:\.element.customID) { (index, item) in Text(String(index) + item) } 回答1: Let's assume we have an Array of objects of type Item : struct Item { let customID: Int let value: String } let arrayNew = [ Item(customID: 1, value: "1"), Item(customID: 23, value: "12"), Item(customID:

sending email with SwiftUI

陌路散爱 提交于 2021-01-28 08:25:48
问题 I'm trying to implement sending email feature into my mini app. Here's the code I'm using (took it from https://hackingwithswift.com): import Foundation import SwiftUI import MessageUI func sendEmail() { if MFMailComposeViewController.canSendMail() { let mail = MFMailComposeViewController() mail.mailComposeDelegate = self mail.setToRecipients(["you@yoursite.com"]) mail.setMessageBody("<p>You're so awesome!</p>", isHTML: true) present(mail, animated: true) } else { // show failure alert } }

SwiftUI: How to get .serif (New York) font in NavBarTitle

不想你离开。 提交于 2021-01-28 07:50:47
问题 is there any simple solution on how to use Apples New York font within the navigationBarTitle? I tried the following, but without success: .navigationBarTitle(Text("TestTitle").font(.system(.largeTitle, design: .serif))) It just defaults back to the default SF font. Any help is greatly appeciated! Thanks! 回答1: Here is a demo of possible solution. Tested with Xcode 12 / iOS 14 init() { UINavigationBar.appearance().largeTitleTextAttributes = [.font: UIFont(descriptor: UIFontDescriptor

How to store a property that conforms to the ListStyle protocol

雨燕双飞 提交于 2021-01-28 07:32:01
问题 Currently I'm setting the listStyle with the .listStyle(InsetGroupedListStyle()) modifier. struct ContentView: View { var body: some View { ListView() } } struct ListView: View { let data = ["One", "Two", "Three", "Four", "Five", "Six"] var body: some View { List { ForEach(data, id: \.self) { word in Text(word) } } .listStyle(InsetGroupedListStyle()) } } I want to make a property inside ListView to store the ListStyle . The problem is that ListStyle is a protocol, and I get: Protocol

Picker in SwiftUI Form turns gray after selecting a value

和自甴很熟 提交于 2021-01-28 07:24:43
问题 For some reason the picker in my SwiftUI Form turns gray after I select a value. My code: Form { Section { TextField("title", text: $title) Picker(selection: $category, label: Text("category")) { ForEach(0..<categories.count) { index in Text(categories[index]).tag(index) } } } } 来源: https://stackoverflow.com/questions/65405759/picker-in-swiftui-form-turns-gray-after-selecting-a-value

How to store a property that conforms to the ListStyle protocol

孤人 提交于 2021-01-28 07:22:52
问题 Currently I'm setting the listStyle with the .listStyle(InsetGroupedListStyle()) modifier. struct ContentView: View { var body: some View { ListView() } } struct ListView: View { let data = ["One", "Two", "Three", "Four", "Five", "Six"] var body: some View { List { ForEach(data, id: \.self) { word in Text(word) } } .listStyle(InsetGroupedListStyle()) } } I want to make a property inside ListView to store the ListStyle . The problem is that ListStyle is a protocol, and I get: Protocol