我正在寻找创建一个可以由视图模型(不仅仅是视图)访问的 EnvironmentObject。
Environment 对象跟踪应用程序会话数据,例如登录、访问令牌等,这些数据将被传递到视图模型(或需要时的服务类)以允许调用 API 以从该 EnvironmentObjects 传递数据。
我试图将会话对象从视图传递给视图模型类的初始化程序,但出现错误。
如何使用 SwiftUI 访问/传递 EnvironmentObject 到视图模型中?
可以在Mac OS X Finder中使用颜色标记文件和文件夹.有没有办法从shell脚本执行此操作?
假设你有一个 UIKit 视图想要决定它自己的大小(通过 internalContentSize 或布局约束,大小可能会改变)。例如:
/// Some UIKit view that wants to have a specific size
class YellowBoxUIKitView : UIView {
init() {
super.init(frame: .zero)
self.backgroundColor = .yellow
}
required init?(coder: NSCoder) {
fatalError("init(coder:) is not supported")
}
override var intrinsicContentSize: CGSize {
return CGSize(width: 100, height: 100)
}
}
Run Code Online (Sandbox Code Playgroud)
如何将其包装为 SwiftUI UIViewRepresentable 并使其自动将 UIView 大小传递给 SwiftUI?
struct YellowBoxView : UIViewRepresentable {
func makeUIView(context: Context) -> YellowBoxUIKitView {
// TODO: How do you pass the size from UIKit up to …
Run Code Online (Sandbox Code Playgroud) ARSessionDelegate
在 ARKit 中使用处理原始相机图像时...
func session(_ session: ARSession, didUpdate frame: ARFrame) {
guard let currentFrame = session.currentFrame else { return }
let capturedImage = currentFrame.capturedImage
debugPrint("Display size", UIScreen.main.bounds.size)
debugPrint("Camera frame resolution", CVPixelBufferGetWidth(capturedImage), CVPixelBufferGetHeight(capturedImage))
// ...
}
Run Code Online (Sandbox Code Playgroud)
...如文档所述,相机图像数据与屏幕尺寸不匹配,例如,在 iPhone XI 上获取:
现在有displayTransform(for:viewportSize:) API 可以将相机坐标转换为视图坐标。当使用这样的 API 时:
let ciimage = CIImage(cvImageBuffer: capturedImage)
let transform = currentFrame.displayTransform(for: .portrait, viewportSize: UIScreen.main.bounds.size)
var transformedImage = ciimage.transformed(by: transform)
debugPrint("Transformed size", transformedImage.extent.size)
Run Code Online (Sandbox Code Playgroud)
我得到的大小为 2340x1920,这似乎不正确,结果的纵横比应为 375:812 (~0.46)。我在这里想念什么/使用此 API 将相机图像转换为“由 ARSCNView 显示”的图像的正确方法是什么?
(示例项目:ARKitCameraImage)
构建 SwiftUI 应用程序的网络层的行之有效的方法是什么?具体来说,您如何使用 URLSession 来构建要在 SwiftUI 视图中显示的 JSON 数据并处理从 iOS 13.4 开始可以正确发生的所有不同状态?
在Ubuntu 16.04服务器(内核4.4.0-22)上,与Ubuntu 14.04相比,根据/ var/log/syslog初始化"random:nonblocking pool"需要2-5分钟:
May 28 18:10:42 foo kernel: [ 277.447574] random: nonblocking pool is initialized
Run Code Online (Sandbox Code Playgroud)
在Ubuntu 14.04(内核3.13.0-79)上发生的速度要快得多:
May 27 06:28:56 foo kernel: [ 14.859194] random: nonblocking pool is initialized
Run Code Online (Sandbox Code Playgroud)
我在DigitalOcean VM上观察到了这一点.它给Rails应用程序带来了麻烦,因为unicorn服务器似乎等待这个池在启动之前变得可用.
这个初始化步骤的合理时间是多少?
为什么在Ubuntu 16.04上需要这么长时间?
应用程序等待此池可用是否合理,或者对池的依赖性是应用程序端的错误?
linux ruby-on-rails ubuntu-server digital-ocean ubuntu-16.04
在以下示例中,有一个 SwiftUI 菜单和一个触发工作表的按钮。如果在菜单可见时按下按钮,可能会导致以下错误,然后按钮将不起作用,并给出以下错误日志(可能需要尝试几次):
2023-03-02 12:01:59.850609+0100 SheetButtonDemo[50295:6802586] [Presentation] Attempt to present <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x124815200> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10c809200> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10c809200>) which is already presenting <_UIContextMenuActionsOnlyViewController: 0x107d0bf20>.
Run Code Online (Sandbox Code Playgroud)
这似乎是 SwiftUI 中的一个错误,我将其报告为 FB12026200。
它与此类似:SwiftUI: popover +sheet in different hierarchies Problem
与此同时:有没有一个好的解决方法来防止这个问题?
示例代码:
2023-03-02 12:01:59.850609+0100 SheetButtonDemo[50295:6802586] [Presentation] Attempt to present <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x124815200> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10c809200> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10c809200>) which is already presenting <_UIContextMenuActionsOnlyViewController: 0x107d0bf20>.
Run Code Online (Sandbox Code Playgroud)
运行中的错误图片:
视图通常不允许可选参数值,从而导致如下错误Initializer 'init(_:)' requires that 'String?' conform to 'StringProtocol'
:
struct Person {
var name : String
}
struct OptionalsExampleView: View {
var person : Person? = Person(name: "Bob")
var body: some View {
VStack() {
Text("Name:")
Text(person?.name)
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,从 Xcode 11.4/iOS 13 开始,if let
View Builder 块中不允许使用该语句,从而导致出现如下错误Closure containing control flow statement cannot be used with function builder 'ViewBuilder'
:
struct OptionalsExampleView: View {
var person : Person? = Person(name: "Bob")
var body: some View { …
Run Code Online (Sandbox Code Playgroud) 是否有可用的所有 SwiftUI 视图类型的列表?
(我问这个问题并自己回答是因为我想为学生提供一个完整的 SwiftUI 视图列表,并且它们不像它们应该的那样容易访问,因此这篇 Stack Overflow 文章)
SwiftUI 阴影与其他内部同级视图重叠:
有没有办法解决这个问题并在这种情况下获得正确的阴影?
视图示例:
// SwiftUIPlayground
// https://github.com/ralfebert/SwiftUIPlayground/
import SwiftUI
struct ShadowsView: View {
var body: some View {
Color.yellow
.ignoresSafeArea()
.overlay(self.scrollView, alignment: .bottom)
}
var scrollView: some View {
VStack {
ForEach(1 ... 3, id: \.self) { _ in
Color.white
.frame(height: 100)
.cornerRadius(10)
.shadow(color: Color.red, radius: 30, x: 0, y: 0)
}
}
.padding()
.frame(height: 200, alignment: .top)
}
}
Run Code Online (Sandbox Code Playgroud) 从 Xcode 11.4 开始,SwiftUI 不允许switch
在 Function builder 块中使用语句,如VStack {}
,失败并出现一般错误,如Generic parameter 'Content' could not be inferred
. 如何switch
在 SwiftUI 中使用该语句根据枚举值创建不同的视图?
swiftui ×8
ios ×4
applescript ×1
arkit ×1
finder ×1
linux ×1
macos ×1
mvvm ×1
swift ×1
ubuntu-16.04 ×1
urlsession ×1