简体   繁体   English

iOS: SwiftUI 创建视图的建议

[英]iOS: SwiftUI suggestion to create a view

I'm trying to make a view in SwiftUI as shown below using SwiftUI.我正在尝试使用 SwiftUI 在 SwiftUI 中创建一个视图,如下所示。

在此处输入图像描述

Below is the code I have written, but I'm not sure what will the best way to code this is.下面是我写的代码,但我不确定最好的编码方式是什么。 My code looks like below with hard-coded values.我的代码如下所示,带有硬编码值。

    var body: some View {
    TestView {
        VStack {
            HStack {
                Image("profile")
                    .padding(.top, 30)
                    .padding(.leading, 30)
                Spacer()
            }
            
            HStack {
                Text("UserName")
                    .font(.system(size: 20, weight: .bold, design: .default))
                    .foregroundColor(.gray)
                    .padding(.leading, 30)
                Spacer()
            }
            .padding(.bottom, 1)
            HStack(spacing: 0) {
                Text("4")
                    .font(.system(size: 42, weight: .bold, design: .default))
                    .foregroundColor(.pink)
                
                VStack {
                    Text("/10")
                        .font(.system(size: 25, weight: .bold, design: .default))
                        .foregroundColor(.gray)
                    Spacer()
                    Spacer()
                    Spacer()
                    Spacer()
                }
            }
            
            Image("stars")
                .resizable()
                .padding(.leading, 20)
                .padding(.trailing, 20)
                .padding(.bottom, 5)
        }
    }
    .frame(width: 200, height: 250, alignment: .center)
}

the output looks like this output 看起来像这样

在此处输入图像描述

I believe this should not be the correct way to do this, with multiple spacers, too many adjustments.我相信这不应该是正确的方法,因为有多个垫片,调整太多。

I'm new to SwiftUI, though the same UI in xib/storyboard won't take me more than 10-15mins with constraints.我是 SwiftUI 的新手,尽管 xib/storyboard 中的相同 UI 不会让我花费超过 10-15 分钟的时间。

Can anyone suggest a better way?谁能提出更好的方法?

PS please ignore color and font size PS请忽略颜色和字体大小

-----------------------------Updated code---------------------------- --------------------------更新代码-------------------- ----------

var body: some View {
    TestView {
        VStack(alignment: .leading) {
            Image("profile")    //Async-image
                .offset(x: 30)
            
            VStack(alignment: .center) {
                Text("UserName")
                    .font(Font(nameFont))
                
                ZStack(alignment: Alignment(horizontal: .leading, vertical: .center)) {
                    HStack(alignment: .firstTextBaseline) {
                        Text("4")
                            .font(Font(bigFont))
                            .foregroundColor(.pink)
                    }
                    HStack(alignment: .firstTextBaseline, spacing: 0) {
                        Text("4")
                            .font(Font(bigFont))
                            .opacity(0)
                        Text("/5")
                            .font(Font(smallFont))
                            .baselineOffset((bigFont.capHeight))
                            .foregroundColor(.gray)
                    }
                }
                
                Image("stars")  //view with rating logic
                    .resizable()
                    .frame(height: 30)
                    .padding(.leading, 20)
                    .padding(.trailing, 20)
                    .padding(.bottom, 5)
            }
        }
    }
    .frame(width: 200, height: 250, alignment: .center)
}

Could avoid Spacers and VStack可以避免SpacersVStack

 HStack(spacing: 0) {
                Text("4")
                    .font(.system(size: 42, weight: .bold, design: .default))
                    .foregroundColor(.pink)
                
                
                Text("/10")
                    .font(.system(size: 25, weight: .bold, design: .default))
                    .foregroundColor(.gray)
             

   .offset(y: -20)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM