I'm using the new SwiftUI Inspector API.
Is it possible to disable the ability to collapse the inspector (when dragging the mouse cursor on the ionspector splitter)?
Also, as a workaround, when the inspector is collapsed, I didn't find how I could expand the inspector programmatically?
import SwiftUI
struct SampleContentView: View {
@State var inspectorPresented = true
var body: some View {
NavigationStack() {
Text("Main View")
.inspector(isPresented: $inspectorPresented, content: {
Text("Inspector")
.inspectorColumnWidth(200)
})
}
}
}
Post
Replies
Boosts
Views
Activity
My question concerns a macOS SwiftUI app. I would like to have a split view with 2 horizontal panes without sidebar: content on the left and inspector on the right. The inspector should act like the inspector panel on the right side side of the Xcode's window: when the window is resized, the inspector keeps it's current width and the user can resize the inspector using the split divider.
Using macOS Monterey SDK, I tried to achieve this with HSplitView but the problem is that the left/right panels are both resized when the window is resized.
struct ContentView: View {
var body: some View {
HSplitView {
Rectangle().fill(.red)
Rectangle().fill(.yellow)
}
}
}
Using Ventura SDK, I just tried the new view NavigationSplitView. I'm using .constant(.doubleColumn) to hide the sidebar but the problem is the same as HSplitView, the left/right panel are both resized when the window is resized.
struct ContentView: View {
var body: some View {
NavigationSplitView(columnVisibility: .constant(.doubleColumn)) {
Text("not used")
} content: {
Rectangle().fill(.red)
} detail: {
Rectangle().fill(.yellow)
}
}
}
When using NSSplitViewController with AppKit, the holdingPriority (https://developer.apple.com/documentation/appkit/nssplitviewitem/1388887-holdingpriority?language=objc) allows to manage this case: The view with the lowest priority is the first to gain additional width if the split view grows or shrinks.
Is it possible to achieve this with SwiftUI?
I'm not sure if I need to store any variable used in updateConfiguration(using:) in the custom key value store of UICellConfigurationState.
I'm skeptical because some of these variables do not semantically represent a "state".
If I look at the sample WWDC sample code for custom cells configurations, all the variables used in updateConfiguration(using:) method are stored in the custom key value store of UICellConfigurationState. In fact, in this sample code, it's the whole model that it's stored in a state custom key.
It seems weird because the base properties of UICellConfigurationState are real "state" properties: like selected, highlighted.
In the same way, for the cells provided by UIKit, non state properties are stored in the configuration. For example, UIListContentConfiguration.imageProperties.tintColor, UIListContentConfiguration.imageToTextPadding.
For a custom cell, we could imagine to subclass UIListContentConfiguration but it's not possible because it's a struct.
Imagine you have a custom UICollectionViewListCell with a custom checkmark view.
Associated to this checkmark, you have: checkmarkTintColor: an “appearance” property to set the tint color of the checkmark.
checkmarkChecked: a ”state” property associated to the checkmark state.
What is the good practice? Storing them the state key value store like this?
var checkmarkChecked: Bool = false {
		didSet {
				guard oldValue != checked else {
						return
				}
				setNeedsUpdateConfiguration()
		}
}
		
var checkmarkTintColor: UIColor? = nil {
		didSet {
				guard oldValue != checkmarkTintColor else {
						return
				}
				setNeedsUpdateConfiguration()
		}
}
var configuration: UIListContentConfiguration? {
		didSet {
				guard oldValue != configuration else {
						return
				}
				setNeedsUpdateConfiguration()
		}
}
override var configurationState: UICellConfigurationState {
		var state = super.configurationState
		state.checkmarkChecked = checkmarkChecked
		state.checkmarkTintColor = checkmarkTintColor
		return state
}
override func updateConfiguration(using state: UICellConfigurationState) {
		...
		checkmarkImageView.image = state.checked ? UIImage(systemName: "checkmark.circle.fill") : UIImage(systemName: "circle")
		checkmarkImageView.tintColor = checkmarkTintColor
}
I wonder how to make an animation on a view inside a cell visible on-screen based on iOS 14 cell configuration API.
Imagine I want to animate the visibility of a placeholder view, in the old style, I would have write something like this:
class MyCell: UICollectionViewCell {
var placeholderVisible: Bool {
get {
return placeholderVisibleBackingValue
}
set {
setPlaceholderVisible(newValue, animated: false)
}
}
func setPlaceholderVisible(_ visible: Bool, animated: Bool) {
placeholderVisibleBackingValue = visible
updatePlaceHolderVisibility(animated: animated)
}
private func updatePlaceHolderVisibility(animated: Bool) {
if animated {
UIView.animate(withDuration: 0.5) {
						 placeholderView.alpha = placeholderVisible ? 1 : 0
				 }
} else {
placeholderView.alpha = placeholderVisible ? 1 : 0
}
}
}
WWDC 2020 "Modern Cell Configuration" session briefly mentions animations at 09:09 time. It says that we can nest a configuration change inside an animation block.
In practice, I'm not sure how I could do this because every configuration change needs to call setNeedsUpdateConfiguration(). Since it's an async method, nesting this method in an animation block would not work.
Here is a code snippet of a custom cell based on a cell configuration API:
class MyCell: UICollectionViewListCell {
		var placeholderVisible: Bool = false {
				didSet {
						guard oldValue != placeholderVisible else {
								return
						}
						setNeedsUpdateConfiguration()
				}
		}
		
		override var configurationState: UICellConfigurationState {
				var state = super.configurationState
				/* `state.placeholderVisible` is a custom key in the UICellConfigurationState key value store */
				state.placeholderVisible = placeholderVisible
				return state
		}
		
		override func updateConfiguration(using state: UICellConfigurationState) {
				var content = defaultListContentConfiguration().updated(for: state)
				listContentView.configuration = content
				placeholderView.alpha = state.placeholderVisible ? 1 : 0
		}
}
Apple recommendation is to always write all the cell’s configuration in a single place updateConfiguration(using state: UICellConfigurationState). Since this method is called by setNeedsUpdateConfiguration()which is async, I don't know how I could apply an animation when changing the alpha value of the placeholderView.
I can't find the API to programmatically check or unckeck the multi select accessory in a UICollectionViewListCell accordingly to the model when the cell is displayed.
This is my cell registration:
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { (cell, indexPath, item) in
var content = cell.defaultContentConfiguration()
content.text = item.title
cell.contentConfiguration = content
		cell.accessories = [.multiselect(displayed: .always, options: UICellAccessory.MultiselectOptions())]
}