How To Position Views Relative To Their Top Left Corner In SwiftUI

前端 未结 2 1629
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 21:10

How do I position views relative to their top left corner in swiftUI? The \"position\" modifier moves the views relative to their center coordinates. So .position(x: 0, y:

2条回答
  •  梦谈多话
    2021-01-21 21:51

    If I correctly understand your goal the .position is not appropriate instrument for it. SwiftUI layout works better without hardcoding.

    Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

    struct HomeView: View {
        var body: some View {
            ZStack(alignment: .topLeading) {
                Color.clear
                VStack(alignment: .leading) {
                    Text("Top Text")
                        .font(.system(size: 20))
                        .fontWeight(.medium)
                     Text("Bottom Text")
                        .font(.system(size: 12))
                        .fontWeight(.regular)
                }
            }.frame(maxWidth: .infinity, maxHeight: .infinity)
        }
    }
    

提交回复
热议问题