【问题标题】:Alignment not working in ios14, xcode12 swiftUI对齐在 ios 14、xcode 12 swiftUI 中不起作用
【发布时间】:2020-10-27 11:47:37
【问题描述】:

我的对齐在 xcode12 ios14 中不起作用,但是,我在 xcode11.5 中有相同的代码,一切都很好。每当我将 .frame viewModifier 添加到内容对齐时,它不在中心,是错误还是 API 已更改?这是代码:请在 xcode12 中查看,仅限 ios14。

struct ContentView: View {
    
    var body : some View {
       
        GeometryReader { geo in
            
            Capsule()
                .fill(Color.red)
                .frame(height: 50)
            
        }
        
    }
    
    
   
}

这也不行:

struct ContentView: View {
    
    var body : some View {
        ZStack(alignment: .center) {
        GeometryReader { geo in
            
            Capsule()
                .fill(Color.red)
                .frame(height: 50)
            
        }
        
    }
    }
    
   
}

这不起作用:

struct ContentView: View {
    
    var body : some View {
        ZStack(alignment: .center) {
        GeometryReader { geo in
            
            Capsule()
                .fill(Color.red)
                .frame(height: 50, alignment: .center)
            
        }
        
    }
    }
    
   
}

每当我删除 GeometryReader 时一切正常,但我想要 GeometryReader

【问题讨论】:

  • 是的,GeometryReader 的行为不同,但很难说哪个更正确。我建议不要依赖 implicit 布局,而是根据需要明确地进行布局。向 Apple 提交反馈,如果需要,他们可能会回复。
  • 我应该回到xcode11.5吗?我正在遵循他们使用xcode12的教程,什么是最佳解决方案
  • 是xcode12还是swiftui2.0还是ios14有问题?

标签: swift swiftui alignment frame ios14


【解决方案1】:

以下内容在两种情况下都为您提供居中的胶囊

ZStack {
    Capsule()
        .fill(Color.red)
        .frame(height: 50)
}.frame(maxWidth: .infinity, maxHeight: .infinity)

【讨论】:

  • 我遇到了和 OP 一样的问题;这个解决方案没有解决问题。还有什么值得一试的吗?
猜你喜欢
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 2020-12-06
  • 2021-01-14
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2014-10-28
相关资源
最近更新 更多