目前正在构建一个聊天应用程序,我需要在屏幕底部显示新消息。我还需要将消息对齐到底部。但是,在 ScrollView 中使用 VStack 默认情况下具有顶部对齐。
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Spacer()
.frame(minWidth: 0, maxWidth: .infinity, minHeight:0, maxHeight: .infinity, alignment: Alignment.topLeading)
ForEach(notList, id: \.self) { not in
NotRow(not: not)
}
}
.padding()
.frame(minWidth: 0, maxWidth: .infinity, minHeight:0, alignment: Alignment.topLeading)
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
我有一个 GraphicalDatePickerStyle 的 DatePicker,我想将它放在矩形的中心。我的代码:
HStack {
Spacer()
DatePicker("", selection: $notificationTimeOnPicker, displayedComponents: .hourAndMinute).foregroundColor(Color("ChartColor")).labelsHidden()
.datePickerStyle(GraphicalDatePickerStyle())
Spacer()
}
Run Code Online (Sandbox Code Playgroud)
这就是我得到和想要的:
如何将 DatePicker 居中?看起来它的框架由于某种未知的原因而很宽。我尝试将其框架大小更改为宽度:95 或更小以将其居中,但之后在某些设备上打字时我得到...而不是数字。
我有一个标签,我花了很多时间尝试将 UILabel 的字体更改为 SF Rounded Bold。
喜欢titleLabel.font = UIFont(name: "SFRounded-Bold", size: 34.0)
或titleLabel.font = UIFont(name: "SanFranciscoRounded-Bold ", size: 34.0)
不工作的事情。
甚至可以在 UIKit 中使用 SF Rounded 吗?它没有列在场景编辑器的字体列表中,也没有人问过如何使用它,但在 SwiftUI 中,我可以毫无问题地使用 SF Rounded。
我有一个大约 100 行的视图:
ForEach(moodEntries.reversed(), id: \.self) { entry in
Button(action: {
self.activeMoodForModal = moodEntries.lastIndex(of: entry) ?? 0
self.showMoodModal.toggle()
}) {
MoodTableViewCard(entry: entry)
}
}.sheet(item: $activeMoodForModal) { item in
MoodEntryModalView(entry: moodEntries[item], saveNote: {
do {
try context.save()
} catch {
print(error)
}
})
}
Run Code Online (Sandbox Code Playgroud)
性能真的很糟糕:当我打开这个视图时,整个应用程序在模拟器上冻结了 3 秒。如果我更改ForEach
为List
一切正常,那么我想延迟加载将解决我的问题。但我不想弄乱 List,所以我尝试将上面提到的所有代码放入其中,LazyVStack { }
但没有帮助:代码的性能和以前一样糟糕。真的LazyVStack
比这更糟糕List
还是我做错了什么?
我有一个模态表,这是代码:
设置仪表板视图:
@State private var notificationsSettingsSheet = false
var body: some View {
Button(action: {
self.notificationsSettingsSheet.toggle()
}) {
VStack(alignment: .leading) {
HStack(alignment: .top, spacing: 4) {
Label("Set Daily Reminders", systemImage: "alarm").foregroundColor(Color("TextColor"))
.font(.system(.headline, design: .rounded))
Spacer()
}
}
}
.sheet(isPresented: $notificationsSettingsSheet) {
NotificationSettingsModal()
}
}
Run Code Online (Sandbox Code Playgroud)
通知设置模式:
var body: some View {
ZStack(alignment: .bottom) {
ScrollView {
VStack(alignment: .leading, spacing: 0) {
Text("Daily Reminders")
.font(.system(.title, design: .rounded))
.fontWeight(.bold)
.padding(.top, headingTopPadding)
.padding(.horizontal, headingHorizontalPadding).foregroundColor(Color("TextColor"))
Spacer().frame(height: 164)
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity) …
Run Code Online (Sandbox Code Playgroud) 尝试使用 iOS 15 的新 Canvas/TimelineView。我尝试使用官方 WWDC 教程创建粒子系统,但无法修复与性能相关的问题。
这是代码:
struct MyView: View {
@State private var count = 32*32
var body: some View {
TimelineView(.animation) { timeline in
Canvas { context, size in
let now = timeline.date.timeIntervalSinceReferenceDate
for i in 0..<count {
context.fill(Ellipse().path(in: CGRect(x: size.width*0.01*Double(i), y: size.height*0.01*Double(i), width: 20.0, height: 20.0)), with: .color(.green))
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它只绘制了 1024 个圆圈,但已经消耗了模拟器 CPU 的 20% 和 iPhone 8 CPU 的 50%。考虑到 iPhone 的强大功能以及新框架的有效性,这是预期的行为吗?如果我需要超过 1024 个圆圈,我应该如何解决这个问题?
我有一个模态表,SwiftUI
但我的应用程序的其余部分是UIKit
。我想在用户点击 中的按钮时显示模式UIKit
。我该如何实施?
目前我有这个代码来显示三个图像:
imshow(image1, title='1')
imshow(image2, title='2')
imshow(image3, title='3')
Run Code Online (Sandbox Code Playgroud)
它工作正常。但是我试图将它们全部放在一行而不是列中。
这是我尝试过的代码:
f = plt.figure()
f.add_subplot(1,3,1)
plt.imshow(image1)
f.add_subplot(1,3,2)
plt.imshow(image2)
f.add_subplot(1,3,3)
plt.imshow(image3)
Run Code Online (Sandbox Code Playgroud)
它抛出
类型错误:无法将 CUDA 张量转换为 numpy。首先使用 Tensor.cpu() 将张量复制到主机内存。
如果我做
f = plt.figure()
f.add_subplot(1,3,1)
plt.imshow(image1.cpu())
f.add_subplot(1,3,2)
plt.imshow(image2.cpu())
f.add_subplot(1,3,3)
plt.imshow(image3.cpu())
Run Code Online (Sandbox Code Playgroud)
它抛出
TypeError: Invalid shape (1, 3, 128, 128) for image data
我应该如何解决这个问题,或者有更简单的方法来实现它?
我有一个 SwiftUI 模态视图,我从主 UIKit 视图调用它。我想在我的模态视图中添加一个关闭按钮。据我所知,UIKit 中没有 @State 变量,所以我正在创建一个单独的 SwiftUI 视图来存储我的 @State 变量,但由于某种原因它不起作用。我应该如何解决这个问题?
我在主 ViewController 中的代码:
var hack = StateInUIKitHack()
hack.modalIsPresented = true
let vc = UIHostingController(rootView: MoodCardView(isPresented: hack.$modalIsPresented, entryIndex: entryIndex, note: moodEntries[entryIndex].note ?? ""))
self.present(vc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
StateInUIKitHack 结构:
struct stateInUIKitHack: View {
@State var modalIsPresented = false
var body: some View {
Text("Hello, World!")
}
}
Run Code Online (Sandbox Code Playgroud)
在 MoodCardView.swift 里面我有:
@Binding var isPresented: Bool
Run Code Online (Sandbox Code Playgroud)
如果我从另一个 SwiftUI 视图创建我的模式表,它会以经典方式关闭,但我需要从 UIKit 视图创建它。
我正在使用此代码创建背景模糊视图:
struct Blur: UIViewRepresentable {
let style: UIBlurEffect.Style = .systemUltraThinMaterial
func makeUIView(context: Context) -> UIVisualEffectView {
return UIVisualEffectView(effect: UIBlurEffect(style: style))
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
uiView.effect = UIBlurEffect(style: style)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,此代码返回一个背景模糊的常规矩形。我怎样才能把它变成圆形?
swift ×9
ios ×8
swiftui ×8
uikit ×3
ios15 ×1
iphone ×1
matplotlib ×1
python ×1
python-3.x ×1
pytorch ×1
swiftui-list ×1
uilabel ×1
vstack ×1