修饰对齐参考线

struct ContentView: View {
    var body: some View {
        VStack(alignment: .menu) {
            HStack {
                Text("Inbox")
                CircleButton()
                    .frame(width: 30, height: 30)
                    .alignmentGuide(.menu) { $0.width / 2 }
            }
            HStack {
                Text("Sent")
                CircleButton()
                    .frame(width: 30, height: 30)
                    .alignmentGuide(.menu) { $0.width / 2 }
            }
            CircleButton()
                .frame(width: 40, height: 40)
        }
    }
    @ViewBuilder
    func CircleButton() -> some View {
        Circle()
            .fill(.gray)
    }
}

struct MenuAlignment: AlignmentID {
    static func defaultValue(in context: ViewDimensions) -> CGFloat {
        context.width / 2
    }
}

extension HorizontalAlignment {
    static let menu = HorizontalAlignment(MenuAlignment.self)
}
效果图:
notion image
 
你觉得这篇文章怎么样?
YYDS
比心
加油
菜狗
views

Loading Comments...