【问题标题】:SwiftUI: Top most element on ZStack is wider than screenSwiftUI:ZStack 上最上面的元素比屏幕宽
【发布时间】:2023-02-26 09:01:56
【问题描述】:

所以我有一个背景比设备宽度宽的 ZStack,但我想把一个 VStack 放在屏幕边界内的顶部。此代码的问题在于 VStack 与 ZStack 中的其他元素一样宽。

关于如何解决这个问题的任何想法?

ZStack {
    Group {
        VStack {
            Image("EmptyPhoto")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .edgesIgnoringSafeArea(.vertical)
                .frame(height: 600)
            Spacer()
        }
        
        Color.black.opacity(0.5)
            .edgesIgnoringSafeArea(.vertical)
        
        VStack {
            Color.black.opacity(0.0)
            
            Spacer()
                .frame(height: 0)
            
            LinearGradient(gradient: Gradient(colors: [Color.black.opacity(0.0), Color.black, Color.black]), startPoint: .top, endPoint: .bottom)
        }
        .edgesIgnoringSafeArea(.vertical)
    } // Background
    
    VStack {
        Picker(selection: $filterPicker, label: EmptyView()) {
            Text("Friends").tag(1)
            Text("World").tag(2)
        }
        .pickerStyle(.segmented)
        
        HStack {
            Text("@gallapagos")
                .foregroundColor(Color.white)
            Spacer()
            
        }
        Spacer()
    }
    .padding()
}

【问题讨论】:

  • 这个怎么样: ZStack{ }.frame(maxWidth: .infinity, maxHeight: .infinity)

标签: ios swift xcode swiftui


【解决方案1】:

通过使用以下内容修复:

extension UIScreen{
   static let screenWidth = UIScreen.main.bounds.size.width
}

把这个放在下面// Background

.frame(maxWidth: UIScreen.screenWidth)

【讨论】:

  • 您知道如何使用这种方法处理屏幕大小调整吗?
【解决方案2】:

我会这样做:

.frame(maxWidth: UIScreen.main.bounds.size.width)

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 2016-10-21
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多