首页 > 解决方案 > SwiftUI:如何让内容与屏幕顶部对齐,但保留后退按钮

问题描述

我正在学习 SwiftUI(使用 Swift 5,针对 iOS 13.2)

问)我如何才能让我的照片(和下面的内容)在缺口区域后面对齐?

到目前为止我在模拟器中得到了什么:

如您所见,我已经了解了如何使用内联导航栏样式。

在此处输入图像描述

我想要的是

在此处输入图像描述

查看代码:

import SwiftUI

struct DrinkDetail: View {
    
    var drink: Drink
    
    var body: some View {
        List {
            ZStack(alignment: .bottom) {
                Image(drink.imageName)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                Rectangle()
                    .frame(height: 80.0)
                    .opacity(0.75)
                    .blur(radius: 10)
                
                HStack {
                    Text(drink.name)
                        .font(.largeTitle)
                        .foregroundColor(.white)
                        .padding(8)
                    Spacer()
                }
            }
                .listRowInsets(EdgeInsets())
            
            Text(drink.description)
                .font(.body)
                .foregroundColor(.primary)
                .lineLimit(nil)
                .padding(.bottom, 50.0)
                .lineSpacing(12)
            
            HStack {
                Spacer()
                Button(action: {}) {
                    Text("Order me")
                }
                    .frame(width: 200.0, height: 50.0)
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
                    
                    
                Spacer()
            }
        }
        .edgesIgnoringSafeArea(.top)
        .navigationBarTitle(Text(""), displayMode: .inline)
    }
}

标签: iosswiftxcodeswiftuialignment

解决方案


看起来这根本不可能,尽管我之前看过的教程显示它是可能的。

在另行通知之前,这是唯一的方法:https ://stackoverflow.com/a/65523676/12299030


推荐阅读