我想通过将函数传递给 .background() 来更改文本背景颜色,如下所示,颜色的值为 0 或 1 或 2 将取决于数据库的数据。我该如何修复它:
import SwiftUI
struct ContentView: View {
@State var color = 0;
var body: some View {
Text("Hello, World!")
.background(changeBkColor(int : self.$color))
}
}
func changeBkColor(int : color)
{
if(color == 1)
{
return Color.red;
}
else if(color == 2)
{
return Color.green;
}
else
{
return Color.blue;
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud) 我ProgressView在 SwiftUI 中创建了一个(使用 Xcode)并进行了一些编辑,但还没有弄清楚如何改变它的高度。
struct ProgressBar: View {
var body: some View {
VStack {
ProgressView("Progres:", value: 50, total: 100)
}.foregroundColor(Color(UIColor.systemBlue))
.scaleEffect(1, anchor: .center)
.accentColor(Color(UIColor.systemGreen))
}
}
Run Code Online (Sandbox Code Playgroud)
我最初问这个问题:
在那里,我有一个List没有部分的。我正在过滤它们,以便它只显示包含 .txt 文件中文本的行TextField。解决方案是将所有内容包装List在Section.
不幸的是,我现在需要过滤Sections。这是我的代码:
struct Group: Identifiable {
let id = UUID() /// required for the List
var groupName = ""
var people = [Person]()
}
struct Person: Identifiable {
let id = UUID() /// required for the List
var name = ""
}
struct ContentView: View {
@State var searchText = ""
var groups = [
Group(groupName: "A People", people: [
Person(name: "Alex"),
Person(name: "Ally"), …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 WKWebView 移植到 SwiftUI。这是我的代码:
import SwiftUI
import WebKit
struct ContentView: View {
var body: some View {
WebViewWrapper()
}
}
/**
WKWebView ported over to SwiftUI with `UIViewRepresentable`.
*/
final class WebViewWrapper: UIViewRepresentable {
/// `UIViewRepresentable` required function #1.
func makeUIView(context: Context) -> WKWebView {
print("make")
let webView = WKWebView() /// EXC_BREAKPOINT error here
return webView
}
/// `UIViewRepresentable` required function #2
func updateUIView(_ uiView: WKWebView, context: Context) {
}
}
Run Code Online (Sandbox Code Playgroud)
就是这样。我创建了一个新的 SwiftUI 项目并将其粘贴进去。但是,我收到此错误:
线程 1:EXC_BREAKPOINT(代码=EXC_I386_BPT,子代码=0x0)
...在控制台中没有打印任何内容。这发生在 iOS 13.0 和 …
我想使用新的 .searchable 修饰符和 .navigationBarDrawer(displayMode: .always) 实现搜索栏。但如果我启动应用程序,搜索栏的默认位置已经“滚动”。
但这就是我想在应用程序启动时实现的目标:
这是我的代码:
struct SearchView: View {
@State var searchText = ""
var body: some View {
NavigationView {
List {
Text("Search Bar")
}
.navigationBarTitle("Search")
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
}
}
}
Run Code Online (Sandbox Code Playgroud) 在这一行中DispatchQueue.main.async我收到此错误:
没有更多上下文,表达类型不明确
import Foundation
class ProductListViewModel: ObservableObject {
@Published var productList = [Product]()
let webService = WebService()
func downloadData(url: URL) async {
do {
let product = try await webService.getData(url: url)
DispatchQueue.main.async {
self.productList = product.map(ProductViewModel.init)
}
} catch {
print("error")
}
}
}
struct ProductViewModel {
let productModel: Product
var id: Int {
productModel.id
}
var title: String {
productModel.title
}
var price: Double {
productModel.price
}
var description: String {
productModel.description
}
var category: Category { …Run Code Online (Sandbox Code Playgroud) 我有这个简单的看法。
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
通常这个预览效果很好。但是今天,我收到此错误Unknown preview provider "ContentView_Previews_":
按下Try Again不起作用。当我按下时Diagnostics,显示:
RemoteHumanReadableError: Failed to update preview.
Error encountered when sending 'previewInstances' message to agent.
==================================
| RemoteHumanReadableError: Unknown preview provider "ContentView_Previews_"
|
| 5SwiftUI does not contain a preview provider named "ContentView_Previews_". Check your build settings to ensure the preview provider …Run Code Online (Sandbox Code Playgroud) 我正在使用 aUIHostingController嵌入ContentView其中ViewController。我想在按下“更改名称”按钮时更改 的ContentView名称。name这是我的代码:
class ViewController: UIViewController {
var contentView: ContentView? /// keep reference to ContentView
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.secondarySystemBackground
/// add the button
let button = UIButton()
button.frame = CGRect(x: 50, y: 50, width: 200, height: 100)
button.setTitle("Change name", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
view.addSubview(button)
/// add the SwiftUI ContentView
let contentView = ContentView()
let hostingController = UIHostingController(rootView: contentView)
self.contentView = …Run Code Online (Sandbox Code Playgroud) 我制作了一个 SemiRoundedRectangle 形状,用于对侧面菜单进行 ClipShape。如果用户使用 RTL 语言,我需要翻转它,但不确定实现此目的的最佳方法。
\n我已经尝试过.flipsForRightToLeftLayoutDirection(true),但这也会翻转实际的阿拉伯文本。当我尝试旋转形状时,它不再符合形状协议,因此我不能再在.clipShape. 当我切换到阿拉伯语时,SwiftUI 中的其他所有内容都会神奇地自行翻转,是否可以在我的形状中添加一些东西来赋予它这些魔力?
感谢您的帮助 :)
\n\nimport SwiftUI\n\nstruct SemiRoundedRectangle: Shape {\n var cornerRadius: CGFloat\n func path(in rect: CGRect) -> Path {\n var path = Path()\n path.move(to: CGPoint(x: rect.maxX, y: rect.minY))\n path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))\n path.addLine(to: CGPoint(x: rect.minX+cornerRadius, y: rect.maxY))\n path.addArc(center: CGPoint(x: cornerRadius, y: rect.height - cornerRadius),\n radius: cornerRadius,\n startAngle: .degrees(90),\n endAngle: .degrees(180), clockwise: false)\n path.addLine(to: CGPoint(x: 0, y: cornerRadius))\n path.addArc(center: CGPoint(x: cornerRadius, y: cornerRadius),\n radius: cornerRadius,\n startAngle: …Run Code Online (Sandbox Code Playgroud) 据我所知,你不能使用if let不可选的变量......
func run() {\n let title = "Hello, world!"\n if let title = title { /// Initializer for conditional binding must have Optional type, not \'String\'\n print(title)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n...但由于某种原因,当它位于闭包内时,它会起作用:
\nfunc run() {\n let title = "Hello, world!"\n let closure = {\n if let title = title { /// No error!\n print(title)\n }\n }\n closure()\n}\nRun Code Online (Sandbox Code Playgroud)\n这是在 Xcode 14.0 beta (14A5228q) 上。这是一个错误,还是一个功能?当我在Xcode 13.3(13E113)上测试时,错误再次出现。
\n顺便说一句,以下 SwiftUI 代码在 Xcode 13 和 Xcode 14 上都可以正常编译:
\n …