SwiftUI DatePicker CompactDatePickerSyle accent color - swiftui

It seems like setting the accent color is not working when the style of a DatePicker is set to CompactDatePickerStyle. It works as expected when using GraphicalDatePickerStyle.
Here's the code I'm using
DatePicker("Start Date", selection: $date, displayedComponents: [.date])
.datePickerStyle(CompactDatePickerStyle())
.accentColor(.red)
.padding()

First, You will need to add the accent color in your Assets folder for it to show up. Second, the color will show only when you run the simulator or external it will not work in the preiview. Looks like it's a bug that needs to be reported...

Related

Make a ContextMenu on TextField / TextEditors in SwiftUI

Hello friends of swift coding,
I try to create a contextMenu on TextEditor and TextField in SwiftUI for my macOS app.
TextEditor(text: $information)
.contextMenu{
Button {
print("gogogo")
} label: {
Text("Just do it!")
}
}
Code above is not working. What I try to achieve: in a TextEditor I like to right-click and either make changes to selected text or paste some textclippings to a certain position.
But the contextMenu inside an editor seems to be different to the contextMenu of other UI-Views.
How is it done, then? Or can I use the above code and have to put it somewhere else or give it a code word?

Can I change a ButtonToggleStyle deselected background color in SwiftUI?

Toggles in SwiftUI have a .tint modifier to set their color when toggled on but I'd like to set a toggle's background color when off, specifically when using .toggleStyle(.button). Is there any way to do so?
Do it in the same way
Toggle("Test", isOn: $isOn)
.toggleStyle(.button)
.tint(.red)
Off state is as expected:

Remove Outline on Right-clicked Rows in SwiftUI Mac App List

I'm building a macOS app with SwiftUI, and I'm trying to remove (or even cover up) the border added to a List item when I right-click it.
Here it is by default:
Now with a right-click and a contextMenu view modifier:
I figured this is an NSTableView quirk, so I tried the approaches in these three Stack Overflow posts:
Customize right click highlight on view-based NSTableView
NSTableView with menu, how to change the border color with right click?
Disabling the NSTableView row focus ring
NSTableView: blue outline on right-clicked rows
I couldn't get any of those to work, and that may be due to the fact that I can't subclass an NSTableView, but can only override its properties and methods with an extension. Here's what I have so far that successfully removes the default table background and such:
extension NSTableView{
open override func viewDidMoveToWindow() {
super.viewDidMoveToWindow()
//Remove default table styles
backgroundColor = NSColor.clear
enclosingScrollView!.drawsBackground = false
selectionHighlightStyle = .none
}
}
Is there any way to remove that right-click border in SwiftUI? I'm even open to covering it with other views, but I can't seem to draw SwiftUI views in that space around the table cell.
I found a workaround for this. I put my List in a ZStack and then set its opacity to zero. I then built out a fully custom version of the same list, but using LazyVStack:
//Message List
ZStack{
//Ghost list for keyboard control
List($model.messages, id: \.self, selection: $model.selectedMessages){ $message in
MessageItemView(message: $message)
}
.focusable()
.opacity(0)
//Custom UI for the above List
ScrollView{
ZStack{
LazyVStack(spacing: 5){
ForEach($model.messagesToday){ $message in
MessageItemView(message: $message)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
Each list is bound to the same model, so if I click a message to select it in the custom UI, the same thing gets selected in the invisible List. All the keyboard shortcuts that come with table use in a List look like they are working on the custom version.
So how does this solve my original problem? You can right-click on the custom MessageItemView and the default ring around the cell is invisible, but the contextMenu still works (it's defined inside my MessageItemView).
This isn't as elegant as I'd like, but it's nice to have 100% control over the UI but still get all the keyboard controls that come for free with a List.

How to assert on the text in a DatePicker which has the compact style?

I am rendering a DatePicker in my application with the compact style as follows:
DatePicker("Date Selected", selection: $selectedDate, displayedComponents: [.date])
.accessibilityIdentifier("DatePicker")
.datePickerStyle(.compact)
I am able to tap on the DatePicker in a UI test, select the date that I want in the calendar-style popup, and then dismiss the popup as follows:
// 1. Show the DatePicker popup
application.datePickers["DatePicker"].tap()
// 2. Choose a date in the popup
application.datePickers.collectionViews.buttons["Friday, January 14"].tap()
// 3. Dismiss the DatePicker popup
application.datePickers["DatePicker"].tap()
(All of the above is demonstrated in this sample application.)
Is it possible to assert that the DatePicker is now showing the date that I selected? If so, how?

SwiftUI: Navigation bar of destination view has no background and is not animated on scroll

I'm trying to make navigation view that leads to destination view with scroll view, where navigation title of destination view would animate towards inline display mode or at least scroll behind the nav bar itself.
Basically I'm trying to replicate behavior of standard Music app, specifically when you go from Library to Songs.
There you have source view (Library) with its own title that is animated into inline display mode on scroll. When you tap Songs you also get list with new title (Songs) that also animates into inline display mode on scroll.
So I have main NavigationView with NavigationBarTitle. I move to destinationView with its own NavigationBarTitle and some long list of content. On scroll, NavigationBarTitle of main Navigation view changes to inline display mode, but NavigationBar of destination view behaves very odd: it's basically an overlay with no background and no animation.
And if you remove NavigationBarTitle of destination view all together it only makes things worse. It seems like it adds another transparent NavigationBar with nothing in it.
Also tried to add background to the navigation bar, looked around documentation, but found no solution.
Not sure if I'm doing something very wrong or it's just beta bug of SwiftUI or Xcode.
import UIKit
struct ContentView: View {
var body: some View {
NavigationView{
List(0..<20) { item in
NavigationLink(destination: DetailedView()) {
Text("Next view")
}
}
.navigationBarTitle("Source View")
}
}
}
struct DetailedView: View {
var body: some View {
List(0...25) { number in
Text("This is \(number)'th row")
}
.navigationBarTitle(Text("Destination View"))
// comment out line above to see empty frame of navigation bar
}
}
This is not a full answer to your question, but a temporary workaround: add top or vertical padding to your list on the child view, depending on your preference. This is what I have been doing until there is a better solution.
This will at least make the content scroll under the navigation header with a proper background rendered behind the header. It doesn't have the nice animation to make the title smaller.
struct DetailedView: View {
var body: some View {
List(0...25) { number in
Text("This is \(number)'th row")
}
.padding(.top)
.navigationBarTitle(Text("Destination View"))
}
}
This is fixed in the iOS 13.1 public release (with the App Store release of Xcode 11).
I'm currently on beta 5, and I think this is an ongoing bug with SwiftUI.
I noticed the same issue while doing the SwiftUI Landmarks Tutorials, and you can easily reproduce the issue: https://imgur.com/a/aYgUUH0
For now, to avoid seeing all the content scroll under a transparent navBar, I've converted all of my Navbars to display as inline, since automatic and large experience the issue.
List {
// ...
}
.navigationBarTitle(Text("MyTitle"), displayMode: .inline)