因此,我一段时间以来一直在尝试找出如何在 SwiftUI 中查找图像的高度,但没有成功。本质上,我有纵向/横向图像,我想要以大约 600 pt 高显示纵向图像,而我想要显示横向图像,因此使用图像的宽度,因此高度是任意的。话虽这么说,我希望它位于 GeometryReader 内部,因为我使用的偏移量使用它,这样当您向下滚动时,图像不会滚动,但如果您向上滚动,它就会滚动。我不相信我可以共享图像,但无论如何,这应该足以成功运行它!
struct ContentView: View {
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
var backButton: some View {
Button(action: {
withAnimation {
self.mode.wrappedValue.dismiss()
}
}) {
Image(systemName: "chevron.left.circle.fill")
.opacity(0.8)
.font(.system(size: 30))
.foregroundColor(.black)
.background(Color.gray.mask(Image(systemName: "chevron.left").offset(x: 9.4, y: 7.6)))
}
}
var body: some View {
ScrollView(showsIndicators: false) {
GeometryReader { geometry in
Image("image1")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: UIScreen.main.bounds.width, minHeight: 350, maxHeight: 600, alignment: .top)
.clipped()
.overlay(
Rectangle()
.foregroundColor(.clear)
.background(LinearGradient(gradient: Gradient(colors: [.clear, .white]), startPoint: .center, endPoint: .bottom))
)
.offset(y: …Run Code Online (Sandbox Code Playgroud) 所以我正在努力使我的应用程序可以部署在 iOS 13(从技术上讲我想要 iOS 13.5)以及 iOS 14 上,所以我编写了这些代码行来测试它:
struct ContentView: View {
var body: some View {
if #available(iOS 14.0, *) {
List {
Text("Cool!")
Text("Cool!")
Text("Cool!")
Text("Cool!")
}
.listStyle(InsetGroupedListStyle())
} else {
List {
Text("Cool!")
Text("Cool!")
Text("Cool!")
Text("Cool!")
}
.listStyle(GroupedListStyle())
.environment(\.horizontalSizeClass, .regular)
}
}
}
Run Code Online (Sandbox Code Playgroud)
构建到 iOS 14 效果很好,但是,每当我在物理手机和模拟器上构建到 iOS 13.5 时,我都会在 AppDelegate 中收到此错误:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
Run Code Online (Sandbox Code Playgroud)
我已经确保将我的 iOS 部署目标设置为 iOS 13.5,并且我使用的是 Xcode 12 beta 2。我尝试使用 beta 1 并得到相同的结果(尽管我使用了相同的文件,所以可能这就是原因?)。我不确定我是否做错了什么,或者这是否是一个错误。当单独使用 iOS 13.5 的代码(没有 if #available)时,它按预期工作,但只有当我添加该检查器时才会出现问题。任何帮助,将不胜感激!
所以我试图在 SwiftUI 的详细信息视图中隐藏导航栏。我在技术上通过在不同的视图中使用 init() 使其工作,但问题是它使整个应用程序的导航栏透明,我只希望它在一个视图中。我没有在 DetailsView 中使用 init() 的原因是因为我有一个需要输入的变量,所以我不知道该怎么做!这是初始化程序的代码:
init() {
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.backgroundColor = .clear
navBarAppearance.barTintColor = .clear
navBarAppearance.tintColor = .black
navBarAppearance.setBackgroundImage(UIImage(), for: .default)
navBarAppearance.shadowImage = UIImage()
}
Run Code Online (Sandbox Code Playgroud)
下面是在 detailsView 中使用 init() 的 Content View 和 Details View 代码的样子:
// 内容视图 //
struct ContentView: View {
var body: some View {
NavigationView {
List {
ForEach(0..<5) { i in
NavigationLink(destination: DetailsView(test: 1)) {
Text("DetailsView \(i)")
}
}
}
.listStyle(InsetGroupedListStyle())
.navigationBarTitle("Test App")
}
}
}
Run Code Online (Sandbox Code Playgroud)
// 详情查看 // …