SwiftUI: Perpetual Diagnostic Error When Building NavigationBar - xcode

I'm new to SwiftUI, and I'm trying to build this nav bar using Xcode 12.4:
Here is the entirety of my view:
struct PreferencesView: View {
var body: some View {
NavigationView {
ZStack {
//Background Color
Color("DosDark")
.edgesIgnoringSafeArea(.all)
Text("Hey.")
//Nav bar styles
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text("Preferences")
.navBarTitleDark()
}
}
}
.navigationBarItems(
leading: NavClose(), //<-- This is where the trouble starts
trailing: NavAbout()
)
}
}
}
}
struct NavClose: View {
var body: some View { //<-- Inexplicable error here
Button(action: {
print("Close...")
}){
Image("close-blue")
}
}
}
struct NavAbout: View {
var body: some View {
Button(action: {
print("Show about stuff...")
}) {
Image("about-blue")
}
}
}
I can get the title to render okay, but as soon as I add the .navigationBarItems bit, I see an error endlessly on my struct that I'm trying to pull in:
When I try putting the Button directly in .navigationBarItems (without using an external struct) I still see the error on that line:
Failed to produce diagnostic for expression; please file a bug report
Am I doing something wrong? Is there a way to make Xcode give me a real error message?

Works fine with Xcode 12.1 / iOS 14.1, but .navigationBarItems was deprecated for the preference of toolbar and probably you have newer version where they are already conflicted.
The solution is to use only toolbar with corresponding placements, like
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
NavClose()
}
ToolbarItem(placement: .navigationBarTrailing) {
NavAbout()
}
ToolbarItem(placement: .principal) {
VStack {
Text("Preferences")
.navBarTitleDark()
}
}
}

Related

.navigationBarTitleDisplayMode(.inline) crashes Xcode preview

I am consistently coming across this issue whenever I use .navigationBarTitleDisplayMode(.inline). Is there a problematic use of this method, or is there something incorrect about my code ?
How to reproduce error:
In the preview, hit the "list.bullet.rectangle.fill" and dismiss the dialog. Do it a second time and now Xcode crashes the Preview.
Xcode Version 14.2 (14C18)
import SwiftUI
struct PDFTableContentsView: View {
var body: some View {
Text("PDF Table Contents View")
.bold()
.underline()
}
}
struct PDFContentView: View {
#State private var showContents: Bool = false
var body: some View {
VStack {
Text(/*#START_MENU_TOKEN#*/"Hello, World!"/*#END_MENU_TOKEN#*/)
}
.navigationTitle("PDF Title")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
showContents.toggle()
} label: {
Image(systemName: "list.bullet.rectangle.fill")
}
}
}
.sheet(isPresented: $showContents) {
PDFTableContentsView()
}
}
}
struct PDFContentView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
PDFContentView()
}
}
}

Change size of Navigation Bar in SwiftUI

Hello SO community 👋🏼
I'm trying to recreate NavigationBar from Contact tab Apple's Phone in my SwiftUI app.
I played around with .toolbar and modifications but really can't recreate it. I wanna replace TextField to SegmentPicker with saving all behavior of .navigationBarTitleDisplayMode(.inline). But don't know is it possible to use SwiftUI only to get it or need to dive into UIKit. I'm not the expert of UIKit and I will be glad for any help. I want use this NavBar in a exact screen in the app and if possible do not change my preferences of NavBar on other part of app. My code:
import SwiftUI
struct NavBar: View {
#State var pickerOptions: Int = 0
var body: some View {
NavigationView {
ScrollView(.vertical) {
VStack {
Text("Hello, World!")
}
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
SegmentPicker
}
ToolbarItem(placement: .navigationBarTrailing) {
Image(systemName: "plus")
}
ToolbarItem(placement: .navigationBarLeading) {
Text("Groups")
}
}
}
}
private var SegmentPicker: some View {
VStack {
Text("Contacts").font(.body.weight(.semibold))
Picker("Options", selection: $pickerOptions) {
Text("Cons").tag(0)
Text("Prons").tag(1)
}
.pickerStyle(.segmented)
.frame(width: 200)
}
}
}

Xcode simulator sporadically not responding to touch events - SwiftUI

Setup: a SwiftUI List with NavigationLinks wrapped in a NavigationView. Tapping on the NavigationLink opens DeviceInfoView only 50% of the time. Environment: Simulator 13.2, Xcode 13.2.1. All works fine on a real device. Any insights? Thanks.
struct DeviceSearchView: View {
#StateObject private var viewModel = DeviceSearchViewModel()
var body: some View {
NavigationView {
Group {
switch viewModel.searchResults {
// ...
case let .success(results):
List(results, id: \.imei) { res in
NavigationLink(destination: DeviceInfoView(imei: res.imei)) {
Text("IMEI: \(res.imei)")
}
}
.listStyle(.insetGrouped)
.navigationTitle("Title")
}
}
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
HStack {
Image(systemName: "cpu")
Text("...").font(.headline)
}
}
}
.searchable(text: $viewModel.searchText, prompt: "IMEI").keyboardType(.numberPad)
}
}
}
...and in the viewModel:
#MainActor
final class DeviceSearchViewModel: ObservableObject {
#Published var searchResults: AsyncResult<[FindySearchResult]> = .empty
// etc...
Simulator -> Device -> Erase all content and settings... fixed it.

SwiftUI: Why .prefersDefaultFocus modifier does not receive focus if there is a TabView on tvOS?

I am developing a tvOS application and I wanted to use view like split screen of course I need focus management in it. I've seen video from WWDC and my research that this can be done with the prefersDefaultFocus(_:in:) modifier. I even made a little demo. While it works fine if TabView is not used on the screen, prefersDefaultFocus(_:in:) does not work when TabView is added.
Have any of you encountered this situation? How can it be overcome?
Here is my code sample, you can test both case;
#main
struct MyApp: App {
var body: some Scene {
WindowGroup {
//ContentView()
ContentWithTabView()
}
}
}
struct ContentView: View {
#Environment(\.resetFocus) var resetFocus
#Namespace private var namespace
var body: some View {
HStack(spacing: 100) {
Group {
VStack {
Button ("1") {}
.prefersDefaultFocus(in: namespace)
Button ("2") {}
Button ("3") {}
}}
Group{
VStack {
Button ("1") {}
Button ("2") {}
Button ("3") {}
Button ("Reset to default focus") {
resetFocus(in: namespace)
}
}
}
}
.focusScope(namespace)
}
}
struct ContentWithTabView: View {
var body: some View {
NavigationView {
TabView {
ContentView()
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
ContentView()
.tabItem {
Image(systemName: "house.fill")
Text("Home")
}
}
}
}
}

SwiftUI Mac Catalyst Scrolling not working with StackNavigationViewStyle

I have the following problem and I hope someone can help me out...
As soon as I use .navigationViewStyle(StackNavigationViewStyle()) the Mac Catalyst App does only scroll for a little while in the "Detailview"
Here my examplecode:
Listview:
struct Liste: View {
var body: some View {
NavigationView {
List {
NavigationLink(destination: DetailView()) {
Text("Hello World")
}
NavigationLink(destination: DetailView()) {
Text("Hello World")
}
NavigationLink(destination: DetailView()) {
Text("Hello World")
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
Detailview:
struct DetailView: View {
var body: some View {
VStack{
Form{
Section{
Text("Hello, World!")
}
}
}
}
}
In the following video, you can see what happens:
Any ideas what I'm doing wrong? Many thanks!

Resources