如何将按钮放置在屏幕底部?斯威夫特用户界面

Chr*_*ris 11 swift swiftui

下面是我在 SwiftUI 中创建视图的代码。我想将“欢迎”按钮放置在屏幕底部,如下所示。

struct welcomeViewControllerView: View {
      var body: some View {
            Text("Welcome")
              .font(.system(size: 20, design: .rounded))
              .padding(.leading)
              .multilineTextAlignment(.center)
              .background(

            Image("splashBackground")
              .resizable()
              .edgesIgnoringSafeArea(.all)
              .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        
            Button(action:{print("button pressed")})
              {
                Text("Continue")
                .font(.system(size: 20, design: .rounded))
                .foregroundColor(.black)
                .frame(width: 300, height: 50, alignment: .center)
               .background((Image("buttonImage")).resizable().frame(width: 300, height: 50, alignment: .center))      
         }
      }
   }

class welcomeViewController: UIHostingController<welcomeViewControllerView> {
        required init?(coder: NSCoder)
         {
             super.init(coder: coder,rootView: welcomeViewControllerView());
          }
    
          override func viewDidLoad()
         {
           super.viewDidLoad()
            view.backgroundColor = .white
         }
}
Run Code Online (Sandbox Code Playgroud)

如何将按钮放置在屏幕底部?我发布了下面的屏幕。我对使用 SwiftUI 还很陌生。

在此输入图像描述

Har*_*tel 21

struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "heart.fill")
                .resizable()
                .frame(width: 60, height: 60)
                .foregroundColor(.red)
            
            Group{
                Text("Welcome!")
                    .font(.title)
                
                Button(action: {
                    print("tapped!")
                }, label: {
                    Text("Continue")
                        .foregroundColor(.white)
                        .frame(width: 200, height: 40)
                        .background(Color.green)
                        .cornerRadius(15)
                        .padding()
                })
            }.frame(maxHeight: .infinity, alignment: .bottom)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述


小智 8

您的问题包括背景图像,下面的代码使用 ZStack 和 Spacer 来获取下面有背景的布局。Spacer 绝对是您水平或垂直推送内容的朋友。

对于背景,因为它大部分是黑色,所以我使用 Color.black 来绘制整个屏幕,并假设您可以使用较小的图像作为徽标。这可以让您发送更小的图像,并且不会变形。分离屏幕颜色还可以让您匹配设备的亮/暗模式设置。如果有兴趣,请参阅 Color(UIColor.systemBackground) 和 @Environment(.colorScheme) 。

ZStack {
    Color.black
    
    Text("Welcome")
        .foregroundColor(.white)
        .font(.title)
    
    VStack {
        Image(systemName:"flame.fill")
            .resizable()
            .aspectRatio(contentMode:.fit)
            .foregroundColor(Color.blue)
            .frame(height:100)
            .padding([.top], 20)
        Spacer() // will spread content to top and bottom of VStack
        Button(action:{print("Continue")}){
            Text("Continue")
                .foregroundColor(.black)
                .padding(10)
                .background(Color.green)
                .cornerRadius(10)
        }
        Spacer()
            .frame(height:50)  // limit spacer size by applying a frame
    }
}
Run Code Online (Sandbox Code Playgroud)

上面为您提供了以下内容: 使用 ZStack 和 Spacer 进行 bkg 布局