创建列表时,以前版本的 Xcode 允许在 iOS 上执行以下操作:
\nList(tabs, selection: $lastTab) {tab in\n\xe2\x80\xa6\n} \nRun Code Online (Sandbox Code Playgroud)\n现在出现错误:
\n'init(_:selection:rowContent:)' is unavailable in iOS\nRun Code Online (Sandbox Code Playgroud)\n也适用于
\nList(tabs, id:\\.self, selection: $lastTab)\nRun Code Online (Sandbox Code Playgroud)\n以及其他变体,例如:
\nList(selection: $lastTab) {\n ForEach(tabs, id:\\.self) {tab in \n...\n}\nRun Code Online (Sandbox Code Playgroud)\n还有其他人遇到过这个问题吗?
\n有人在 Xcode 12b 的 SwiftUI 2 中看到 @State 变量没有更新的问题吗?问题代码在 contextMenu 按钮中。
该showSheet变量被切换为true,但sheetItem值不会改变。相同的代码在 SwiftUI 1 中有效。
我已经提交了反馈,但想知道是否有其他人看到过这个问题。
完整代码:
struct ConnectionsView: View {
@EnvironmentObject var connectionViewModel : FileDataViewModel<Connection>
@State private var editMode = EditMode.inactive
@State private var importedConnections = false
@State private var showEditSheet = false
@State private var selectedIndex = 0
@State private var showSheet = false
@State private var sheetItem = SheetItem.openFile //TOD: Set back to .none when ButtonActions fixed
@State private var newConnection = Connection() …Run Code Online (Sandbox Code Playgroud) 我已经在我的 Mac 应用程序中成功实现了 10.11 版本的 NSCollectionView。它显示了我想要的 10 个项目,但我希望在应用程序启动时自动选择第一个项目。
我在 viewDidLoad 和 viewDidAppear 函数中尝试了以下操作;
let indexPath = NSIndexPath(forItem: 0, inSection: 0)
var set = Set<NSIndexPath>()
set.insert(indexPath)
collectionView.animator().selectItemsAtIndexPaths(set, scrollPosition: NSCollectionViewScrollPosition.Top)
Run Code Online (Sandbox Code Playgroud)
我已经尝试过上面的第 4 行,无论有没有动画师
我还尝试了以下方法来代替第 4 行
collectionView.animator().selectionIndexPaths = set
Run Code Online (Sandbox Code Playgroud)
有和没有 animator()
虽然它们都在选定的索引路径中包含索引路径,但实际上都没有将项目显示为选定的。
有什么线索我哪里出错了吗?
我正在创建一个 Swift 包,它将在指定的时间内闪烁一些文本(如 toast 实现)。我希望用户可以选择在调用该项目时指定背景形状,但是当我尝试创建一个Shape 参数,我在声明行收到编译错误(错误 1):
协议“形状”只能用作通用约束,因为它具有自身或关联的类型要求
以及我尝试使用它的地方(错误2):
协议“形状”作为一种类型不能符合协议本身
import SwiftUI
public struct Toast: View {
@Binding var show: Bool
var message: String = ""
var duration: Double = 2.0
var fontSize: Font = .title
var textColor: Color = Color(.secondaryLabel)
var backgroundColor : Color = Color (.clear)
var encapsulate: Bool = false
var shape: Shape = Capsule() //Error 1
public init(show: Binding<Bool>,
message: String,
duration: Double = 2.0,
fontSize: Font = .title,
textColor: Color = Color(.secondaryLabel),
backgroundColor: Color = …Run Code Online (Sandbox Code Playgroud) 我创建了一个 Swift 包,它根据传递给它的内容数组创建多个 PageTabView:
import SwiftUI
public struct WhatsNewView<Content: View>: View {
let content: [Content]
public init(content: [Content]){
self.content = content
}
public var body: some View {
TabView {
ForEach(0..<content.count, id: \.self) { pageNum in
WhatsNewPage(content: content[pageNum], pageNum: pageNum + 1, totalPages: content.count)
}
}
.background(Color.white)
.ignoresSafeArea()
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用它时,我想传递任何类型的简单或复杂视图来填充每个页面。现在,我可以传入一组简单的文本视图,如下所示:
import SwiftUI
import WhatsNew
@main
struct mFood_Vendor: App {
@State var showWhatsNew = false
let whatsNew = WhatsNew()
var page1 = Text("Hello World")
var page2 = …Run Code Online (Sandbox Code Playgroud) 我有以下代码,其中 SplashScreen 的内容平滑淡出。但颜色(“app_color”)视图似乎比图像视图淡出得更快。
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
TabbedView()
SplashScreen (duration: 2.0, delay: 2.0) {
SplashContents()
}
}
}
}
struct SplashContents: View {
var body: some View {
ZStack {
Color("app_color")
Image("app_logo")
.resizable()
.frame(width: 200, height: 200)
}
}
}
public struct SplashScreen<Content: View>: View {
@State private var showSplash = true
let duration: Double
let delay: Double
let content: Content
public init(duration: Double = 0, delay: Double = 0, @ViewBuilder …Run Code Online (Sandbox Code Playgroud)