当列表中的最后一项是 HStack 时,应用程序崩溃
When last item in list is HStack, app crashes
我正在做一个关于我的学校建筑和楼层的应用程序,我使用导航视图,但是,我不想查看披露指示器,所以我找到 "hack" 来设置导航 link 宽度为 0 和不透明度。当我也想用最后一个 NavigationLink 执行此操作时,我的应用程序在初始化期间访问错误的 App Delegate 文件中崩溃。当我不在最后一项上使用 HStack 和这个 "hack" 时,它看起来像其他列表行,但不会执行任何操作。只有当下一个视图位于 Navigation Link 内时,它才起作用,这会导致主屏幕的最后一个 List 行看起来不同。有任何想法吗?非常感谢你。
import SwiftUI
struct BuildingsView: View {
init() {
UITableView.appearance().separatorStyle = .none
}
var body: some View {
NavigationView {
List {
HStack{
BuildingCardView(titleString: "Námestie J. Herdu", subtitleString: "Námestie J. Herdu 2, Trnava", imageString: "namjherdu", infoString: "Rektorát, FMK, FF, FPV, internáty, jedáleň, kancelárie")
NavigationLink(destination: NamJHerduView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "Hajdóczyho", subtitleString: "Jána Hajdóczyho 1, Trnava", imageString: "hajdoczy", infoString: "Knižnica, učebne, kancelárie, Kino OKO")
NavigationLink(destination: HajdoczyhoView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "Bučianska", subtitleString: "Bučianska 4A, Trnava", imageString: "bucianska", infoString: "FSV, FMK, aula, kancelárie")
NavigationLink(destination: BucianskaView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "V Jame", subtitleString: "V Jame 3, Trnava", imageString: "vjame", infoString: "FMK, FSV, jedáleň")
NavigationLink(destination: VJameView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "Skladová", subtitleString: "Skladová 3, Trnava", imageString: "skladova", infoString: "FMK")
NavigationLink(destination: SkladovaView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
NavigationLink(destination: SpacinceView()) {
BuildingCardView(titleString: "Špačince", subtitleString: "Hlavná 6, Špačince", imageString: "spacince", infoString: "FPV, výskumné laboratóriá")}
}
.navigationBarTitle(
Text("Budovy UCM"), displayMode: .large).navigationBarHidden(false)
}
}
}
import SwiftUI
struct BuildingCardView: View {
let titleString: String?
let subtitleString: String?
let imageString: String?
let infoString: String?
init(titleString: String? = "null", subtitleString: String? = "null", imageString: String? = "default", infoString: String? = "null"){
self.titleString = titleString
self.subtitleString = subtitleString
self.imageString = imageString
self.infoString = infoString
}
var body: some View {
VStack {
Image(imageString!)
.resizable()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 150, alignment: .topLeading)
HStack {
VStack(alignment: .leading) {
Text(titleString!)
.font(.title)
.fontWeight(.black)
.foregroundColor(.primary)
.lineLimit(1)
Text(subtitleString!)
.font(.headline)
.foregroundColor(.secondary)
.lineLimit(3)
Text(infoString!)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(3)
}
.layoutPriority(100)
Spacer()
}
.padding()
}
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.2), lineWidth: 1)
)
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
TabView{
BuildingsView()
.tabItem {
Image(systemName: "house")
Text("Budovy")
}
Text("Vyhľadávanie TBD")
.tabItem {
Image(systemName: "magnifyingglass")
Text("Vyhľadávanie")
}
MapView()
.tabItem {
Image(systemName: "map")
Text("Mapa")
}
}
}
}
至少应该解决五个问题:
- 你的最后一张牌看起来不一样了
- "inline-mode" 中的导航栏不会转到屏幕的上角(您可以在屏幕截图上看到)
- 您可以单击相应卡片周围的边框来打开 NavigationLink
。
- 图像被拉伸
- 如果您想在任何地方毫无问题地使用列表,您现在就可以做到。在我修复之前,这些列表将不再有分隔符。
但是请注意如果你想改变ContentView
的背景颜色,我的建议UIScrollView.appearance().backgroundColor = UIColor(named: "CustomScrollViewBackgroundColor")
并不总是正确的。因此,如果您对此有任何疑问,我可以向您推荐 this and this 个问题。
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
NavigationView {
BuildingsView()
}
.tabItem {
Image(systemName: "house")
Text("Budovy")
}
Text("Vyhľadávanie TBD")
.tabItem {
Image(systemName: "magnifyingglass")
Text("Vyhľadávanie")
}
MapView()
.tabItem {
Image(systemName: "map")
Text("Mapa")
}
} .edgesIgnoringSafeArea(.top)
}
}
struct BuildingsView: View {
var body: some View {
// UIScrollView.appearance().backgroundColor = UIColor(named: "CustomScrollViewBackgroundColor") // If you want to change the background color
return ScrollView {
VStack {
NavigationLink(destination: NamJHerduView()) {
BuildingCardView(titleString: "Námestie J. Herdu", subtitleString: "Námestie J. Herdu 2, Trnava", imageString: "namjherdu", infoString: "Rektorát, FMK, FF, FPV, internáty, jedáleň, kancelárie")
} .buttonStyle(PlainButtonStyle()) // This is needed because the system considers the content in the NavigationLink as a button. And buttons are colored blue by system default.
NavigationLink(destination: HajdoczyhoView()) {
BuildingCardView(titleString: "Hajdóczyho", subtitleString: "Jána Hajdóczyho 1, Trnava", imageString: "hajdoczy", infoString: "Knižnica, učebne, kancelárie, Kino OKO")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: BucianskaView()) {
BuildingCardView(titleString: "Bučianska", subtitleString: "Bučianska 4A, Trnava", imageString: "bucianska", infoString: "FSV, FMK, aula, kancelárie")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: VJameView()) {
BuildingCardView(titleString: "V Jame", subtitleString: "V Jame 3, Trnava", imageString: "vjame", infoString: "FMK, FSV, jedáleň")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: SkladovaView()) {
BuildingCardView(titleString: "Skladová", subtitleString: "Skladová 3, Trnava", imageString: "skladova", infoString: "FMK")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: SpacinceView()) {
BuildingCardView(titleString: "Špačince", subtitleString: "Hlavná 6, Špačince", imageString: "spacince", infoString: "FPV, výskumné laboratóriá")
} .buttonStyle(PlainButtonStyle())
} .padding(.horizontal).padding(.bottom)
}
.navigationBarTitle(Text("Budovy UCM"))
}
}
struct BuildingCardView: View {
let titleString: String?
let subtitleString: String?
let imageString: String?
let infoString: String?
init(titleString: String? = "null", subtitleString: String? = "null", imageString: String? = "default", infoString: String? = "null"){
self.titleString = titleString
self.subtitleString = subtitleString
self.imageString = imageString
self.infoString = infoString
}
var body: some View {
VStack(spacing: 0) {
Image(imageString!)
.resizable()
.aspectRatio(contentMode: .fit) // You should not use frame for resizing the image otherwise it be will stretched
HStack {
VStack(alignment: .leading, spacing: 0) {
Text(titleString!)
.font(.title)
.fontWeight(.black)
.foregroundColor(.primary)
.lineLimit(1)
Text(subtitleString!)
.font(.headline)
.foregroundColor(.secondary)
.lineLimit(3)
Text(infoString!)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(3)
}
.layoutPriority(100)
Spacer()
}
.padding(.all, 16)
// .background(Color("CustomCardBackgroundColor")) // If you want to change the background color of your cards
}
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.2), lineWidth: 1)
)
}
}
我正在做一个关于我的学校建筑和楼层的应用程序,我使用导航视图,但是,我不想查看披露指示器,所以我找到 "hack" 来设置导航 link 宽度为 0 和不透明度。当我也想用最后一个 NavigationLink 执行此操作时,我的应用程序在初始化期间访问错误的 App Delegate 文件中崩溃。当我不在最后一项上使用 HStack 和这个 "hack" 时,它看起来像其他列表行,但不会执行任何操作。只有当下一个视图位于 Navigation Link 内时,它才起作用,这会导致主屏幕的最后一个 List 行看起来不同。有任何想法吗?非常感谢你。
import SwiftUI
struct BuildingsView: View {
init() {
UITableView.appearance().separatorStyle = .none
}
var body: some View {
NavigationView {
List {
HStack{
BuildingCardView(titleString: "Námestie J. Herdu", subtitleString: "Námestie J. Herdu 2, Trnava", imageString: "namjherdu", infoString: "Rektorát, FMK, FF, FPV, internáty, jedáleň, kancelárie")
NavigationLink(destination: NamJHerduView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "Hajdóczyho", subtitleString: "Jána Hajdóczyho 1, Trnava", imageString: "hajdoczy", infoString: "Knižnica, učebne, kancelárie, Kino OKO")
NavigationLink(destination: HajdoczyhoView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "Bučianska", subtitleString: "Bučianska 4A, Trnava", imageString: "bucianska", infoString: "FSV, FMK, aula, kancelárie")
NavigationLink(destination: BucianskaView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "V Jame", subtitleString: "V Jame 3, Trnava", imageString: "vjame", infoString: "FMK, FSV, jedáleň")
NavigationLink(destination: VJameView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
HStack {
BuildingCardView(titleString: "Skladová", subtitleString: "Skladová 3, Trnava", imageString: "skladova", infoString: "FMK")
NavigationLink(destination: SkladovaView()) {
EmptyView()}
.frame(width: 0)
.opacity(0)
}
NavigationLink(destination: SpacinceView()) {
BuildingCardView(titleString: "Špačince", subtitleString: "Hlavná 6, Špačince", imageString: "spacince", infoString: "FPV, výskumné laboratóriá")}
}
.navigationBarTitle(
Text("Budovy UCM"), displayMode: .large).navigationBarHidden(false)
}
}
}
import SwiftUI
struct BuildingCardView: View {
let titleString: String?
let subtitleString: String?
let imageString: String?
let infoString: String?
init(titleString: String? = "null", subtitleString: String? = "null", imageString: String? = "default", infoString: String? = "null"){
self.titleString = titleString
self.subtitleString = subtitleString
self.imageString = imageString
self.infoString = infoString
}
var body: some View {
VStack {
Image(imageString!)
.resizable()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 150, alignment: .topLeading)
HStack {
VStack(alignment: .leading) {
Text(titleString!)
.font(.title)
.fontWeight(.black)
.foregroundColor(.primary)
.lineLimit(1)
Text(subtitleString!)
.font(.headline)
.foregroundColor(.secondary)
.lineLimit(3)
Text(infoString!)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(3)
}
.layoutPriority(100)
Spacer()
}
.padding()
}
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.2), lineWidth: 1)
)
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
TabView{
BuildingsView()
.tabItem {
Image(systemName: "house")
Text("Budovy")
}
Text("Vyhľadávanie TBD")
.tabItem {
Image(systemName: "magnifyingglass")
Text("Vyhľadávanie")
}
MapView()
.tabItem {
Image(systemName: "map")
Text("Mapa")
}
}
}
}
至少应该解决五个问题:
- 你的最后一张牌看起来不一样了
- "inline-mode" 中的导航栏不会转到屏幕的上角(您可以在屏幕截图上看到)
- 您可以单击相应卡片周围的边框来打开 NavigationLink
。
- 图像被拉伸
- 如果您想在任何地方毫无问题地使用列表,您现在就可以做到。在我修复之前,这些列表将不再有分隔符。
但是请注意如果你想改变ContentView
的背景颜色,我的建议UIScrollView.appearance().backgroundColor = UIColor(named: "CustomScrollViewBackgroundColor")
并不总是正确的。因此,如果您对此有任何疑问,我可以向您推荐 this and this 个问题。
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
NavigationView {
BuildingsView()
}
.tabItem {
Image(systemName: "house")
Text("Budovy")
}
Text("Vyhľadávanie TBD")
.tabItem {
Image(systemName: "magnifyingglass")
Text("Vyhľadávanie")
}
MapView()
.tabItem {
Image(systemName: "map")
Text("Mapa")
}
} .edgesIgnoringSafeArea(.top)
}
}
struct BuildingsView: View {
var body: some View {
// UIScrollView.appearance().backgroundColor = UIColor(named: "CustomScrollViewBackgroundColor") // If you want to change the background color
return ScrollView {
VStack {
NavigationLink(destination: NamJHerduView()) {
BuildingCardView(titleString: "Námestie J. Herdu", subtitleString: "Námestie J. Herdu 2, Trnava", imageString: "namjherdu", infoString: "Rektorát, FMK, FF, FPV, internáty, jedáleň, kancelárie")
} .buttonStyle(PlainButtonStyle()) // This is needed because the system considers the content in the NavigationLink as a button. And buttons are colored blue by system default.
NavigationLink(destination: HajdoczyhoView()) {
BuildingCardView(titleString: "Hajdóczyho", subtitleString: "Jána Hajdóczyho 1, Trnava", imageString: "hajdoczy", infoString: "Knižnica, učebne, kancelárie, Kino OKO")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: BucianskaView()) {
BuildingCardView(titleString: "Bučianska", subtitleString: "Bučianska 4A, Trnava", imageString: "bucianska", infoString: "FSV, FMK, aula, kancelárie")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: VJameView()) {
BuildingCardView(titleString: "V Jame", subtitleString: "V Jame 3, Trnava", imageString: "vjame", infoString: "FMK, FSV, jedáleň")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: SkladovaView()) {
BuildingCardView(titleString: "Skladová", subtitleString: "Skladová 3, Trnava", imageString: "skladova", infoString: "FMK")
} .buttonStyle(PlainButtonStyle())
NavigationLink(destination: SpacinceView()) {
BuildingCardView(titleString: "Špačince", subtitleString: "Hlavná 6, Špačince", imageString: "spacince", infoString: "FPV, výskumné laboratóriá")
} .buttonStyle(PlainButtonStyle())
} .padding(.horizontal).padding(.bottom)
}
.navigationBarTitle(Text("Budovy UCM"))
}
}
struct BuildingCardView: View {
let titleString: String?
let subtitleString: String?
let imageString: String?
let infoString: String?
init(titleString: String? = "null", subtitleString: String? = "null", imageString: String? = "default", infoString: String? = "null"){
self.titleString = titleString
self.subtitleString = subtitleString
self.imageString = imageString
self.infoString = infoString
}
var body: some View {
VStack(spacing: 0) {
Image(imageString!)
.resizable()
.aspectRatio(contentMode: .fit) // You should not use frame for resizing the image otherwise it be will stretched
HStack {
VStack(alignment: .leading, spacing: 0) {
Text(titleString!)
.font(.title)
.fontWeight(.black)
.foregroundColor(.primary)
.lineLimit(1)
Text(subtitleString!)
.font(.headline)
.foregroundColor(.secondary)
.lineLimit(3)
Text(infoString!)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(3)
}
.layoutPriority(100)
Spacer()
}
.padding(.all, 16)
// .background(Color("CustomCardBackgroundColor")) // If you want to change the background color of your cards
}
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.2), lineWidth: 1)
)
}
}