给出如下所示的HStack:
did you mean...
如何强制“中心”视图居中?
答案 0 :(得分:4)
这里可能是简单的方法。使用Xcode 11.4 / iOS 13.4进行了测试
...
const submitted_at = threads[0].data.submitted_at.toDate();
const ref = firestore()
.collection('Discover')
.where('submitted_at', '>', `${submitted_at}`)
// Also tried comparing using new Date(submitted_at) but it still did not work
.orderBy('submitted_at', 'desc')
.limit(10);
...
中提供了带有左对齐/右对齐的解决方案的解决方案
答案 1 :(得分:3)
答案需要几个步骤
对于必须填充VStack的视图,我使用了Geometry Reader。这样会自动扩展为采用父级的尺寸,而不会干扰布局。
import SwiftUI
//Custom Alignment Guide
extension HorizontalAlignment {
enum SubCenter: AlignmentID {
static func defaultValue(in d: ViewDimensions) -> CGFloat {
d[HorizontalAlignment.center]
}
}
static let subCentre = HorizontalAlignment(SubCenter.self)
}
struct CentreSubviewOfHStack: View {
var body: some View {
//VStack Alignment set to the custom alignment
VStack(alignment: .subCentre) {
HStack{
Text("View1")
//Centre view aligned
Text("Centre")
.alignmentGuide(.subCentre) { d in d.width/2 }
Text("View2")
Text("View3")
}
//Geometry reader automatically fills the parent
//this is aligned with the custom guide
GeometryReader { geometry in
EmptyView()
}
.alignmentGuide(.subCentre) { d in d.width/2 }
}
}
}
struct CentreSubviewOfHStack_Previews: PreviewProvider {
static var previews: some View {
CentreSubviewOfHStack()
.previewLayout(CGSize.init(x: 250, y: 100))
}
}