【问题标题】:How to resize a background Image in SwiftUI without affecting the screen views?如何在 SwiftUI 中调整背景图像的大小而不影响屏幕视图?
【发布时间】:2021-03-20 12:42:19
【问题描述】:

我有一个如下所示的内容视图:

内容视图的代码是:

    var body: some View {
    ZStack(alignment: .bottom) {
        
        VStack {
            if selectedTab == "gearshape" {
                GearshapeView()
            }
        }
        CustomTabBar(selectedTab: $selectedTab)
    }
}

这是 GearshapeView 的代码:

 var body: some View {

Color.blue
.edgesIgnoringSafeArea(.all)

}

现在我想在背景中添加一张图片,并保持菜单的大小和以前一样。

    var body: some View {

            Image("s7")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .ignoresSafeArea()

       }

不幸的是,当我调整图片大小以适应屏幕时,菜单也会调整大小。

关于如何保持静态而不是拉伸的任何想法?

【问题讨论】:

  • TabBar 的代码在哪里
  • 认为它是固定的,所以你不需要它。您也可以使用一个简单的按钮对其进行测试,并看到相同的结果

标签: swift image swiftui resizable


【解决方案1】:

使用几何阅读器解决了这个问题:

  GeometryReader { geo in
            Image("s7")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: geo.size.width, height: geo.size.height)
    }
                .edgesIgnoringSafeArea(.all)
            

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多