【问题标题】:How to scale the contentview to multiple screen sizes? SwiftUI如何将内容视图缩放到多个屏幕尺寸? SwiftUI
【发布时间】:2019-11-07 15:56:46
【问题描述】:

新手来了!我正在使用 Swiftui 构建一个测验应用程序,我通过在 iPhone 11 模拟器中预览它来构建视图控制器。

而且我认为 controlview 会适合其他 iPhone 尺寸,例如 iPhone 8。因为 Swiftui 具有内置的自动布局。

但是当我运行 iPhone 8 模拟器时,控件视图中的某些内容不可见,因为它们位于屏幕下方。

有办法解决吗?

我尝试使用多个 Spacer() 和不同的填充,但我似乎无法让它同时在两个屏幕上看起来不错。

这是我的代码:

import SwiftUI

struct questionOne: View {


    @State var totalClicked: Int = 0
    @State var showDetails = false
    @State var isSelected = false

    var body: some View {
        VStack {

            TRpic().frame(width: 350.0, height: 233.0).cornerRadius(10).padding(.top, 80)

            Spacer()

            Text(verbatim: "What's the capital of Turkey?")
                .font(.title)
                .padding(.bottom, 60)
                .frame(height: 100.0)




            Button(action: {}) {

                Text("Istanbul")
            }.buttonStyle(MyButtonStyle())


            Spacer()

            Button(action: {self.isSelected.toggle()}) {
                Text("Ankara")
            }.buttonStyle(SelectedButtonStyle(isSelected: $isSelected))


            Spacer()

            Button(action: {}) {
                Text("Athens")

            } .buttonStyle(MyButtonStyle())

            Spacer()



            NavigationLink(destination: questionTwo()) {
        VStack {
                Text("Next Question")

               Adview().frame(width: 150, height: 60)
                }

               }

            }.edgesIgnoringSafeArea(.top)
        }
    }

struct MyButtonStyle: ButtonStyle {

  func makeBody(configuration:

    Self.Configuration) -> some View {
    configuration.label
      .padding(20)
      .foregroundColor(.white)
      .background(configuration.isPressed ? Color.red : Color.gray)
      .cornerRadius(10.0)
  }
}

struct SelectedButtonStyle: ButtonStyle {

    @Binding var isSelected: Bool

    public func makeBody(configuration: Self.Configuration) -> some View {
        configuration.label
            .padding(20)
            .foregroundColor(.white)
            .background(isSelected ? Color.green : Color.gray)
            .cornerRadius(10.0)
    }
}

enter image description here

Screenshot

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    在给定的上下文中我猜你不想要滚动视图,所以关于间距我建议使用带有间距参数 VStack(alignment: .center, spacing: n){ ... }VStack 并删除 Spacers,如果在 2 个视图之间你需要比 n 其他距离,只需使用padding 添加一些额外的空间。 这应该调整所有内容以适应任何屏幕的高度,包括图像,因此不需要固定框架。

    但是,您可能有一个非常宽的图像,可能超出安全区域,因此,您可以将图像的最大宽度设置为屏幕宽度

    struct questionOne: View {
    
       var body: some View {
            GeometryReader { geometryProxy in
                VStack(alignment: .center, spacing: 20) {
    
                    TRpic().frame(maxWidth: geometryProxy.size.width, alignment: .center)
                           .padding([.leading, .trailing], 10)
                      .......               
                }  
            }
       }
    

    【讨论】:

    • 我猜是固定框架。我删除了“.frame(width: 350.0, height: 233.0)”,整个视图都适合!谢谢!但问题是我有 50 个这样的控制视图,其中包含 50 张不同的图片。我使用固定框架来获得标准外观。你对如何做到这一点有什么建议吗?
    • 即使我继续使用固定框架。正如您从屏幕截图中看到的(我在问题中的代码下方添加了一个链接),控制视图中有足够的空间。我只是觉得 Spacer() 工作不均匀。
    • 我按照您的建议进行了更改,看起来比以前好多了。 iPhone 11 的屏幕看起来有点奇怪,我在代码下方贴了一个新的屏幕截图。
    • @IrfanKaya A Spacer()NavigationLink 之前应该可以解决问题,它会强制VStack 使用整个屏幕
    • 使用 Spacer() iPhone 11 的屏幕看起来不错,但它会扰乱 iPhone 8 的视图。感谢您的耐心和努力。我只是不认为它会以这种方式工作。我必须改变整个事情。我认为 Apple 希望人们使用滚动视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多