【发布时间】:2021-08-09 12:02:12
【问题描述】:
我正在尝试在 MacOS 应用程序中使用 swiftUI 制作菜单。
基于此:
我正在使用这个特定的代码
struct SelectionRow: View {
var title: String
var isSelected: Bool
var action: () -> Void
var body: some View {
Button(action: self.action) {
HStack {
if self.isSelected {
Image("checkmark")
.resizable()
.renderingMode(.template)
.scaledToFit()
} else {
//FIXME: What to add here ???!!! <-- Need to add solution here I believe
}
Text(self.title)
//.frame(alignment: .trailing). <-- Not effective
}
}.foregroundColor(Color.black)
}
}
菜单项条目:
Menu("Example Menu"){
SelectionRow(title: "One", isSelected: true, action: { print("hello world")})
SelectionRow(title: "Two", isSelected: false, action: { print("hello world")})
SelectionRow(title: "Three", isSelected: true, action: { print("hello world")})
}
如果您查看图像“二”菜单项与“一”和“三”菜单选项不对齐,因为选项“一”和“三”有一个复选标记图像。 我尝试了具有最小宽度的 Spacer(以及其他一些选项),但似乎不起作用并将“二”与“一”和“三”对齐
您能推荐任何可以解决此问题并使“二”与“一”和“三”对齐的方法吗?
【问题讨论】:
-
只需为复选标记图像设置框架,如果未选中则添加具有相同框架的空视图
-
试过了,用EmptyView().frame(width: 30),Image用同一个frame,还是不行。不知道为什么。可能是菜单不接受它