I'm developing a macOS application and facing an issue with NSTextField delegates after refactoring my code. Here's the situation:
I have an NSWindowController.
Inside the NSWindowController, there's a container NSView named containerView.
On top of containerView, I added a custom NSView subclass named MyDetailsView.
MyDetailsView has two NSTextField instances, and their delegates are properly set. The delegate methods like controlTextDidChange(_:) were getting called as expected.
Due to some additional requirements, I refactored MyDetailsView into MyDetailsViewController, a subclass of NSViewController.
I created a corresponding .xib file for MyDetailsViewController.
Updated the code to load and add MyDetailsViewController's view (view property) to containerView.
Verified that the NSTextField delegates are still being set, and the fields are displayed correctly in the UI.
However, after this refactor, the NSTextField delegate methods (e.g., controlTextDidChange(_:)) are no longer being triggered.
**What I've Tried: **
Verified that the delegates for the NSTextField instances are correctly set after the refactor. Ensured that the MyDetailsViewController's view is added to containerView.
Question: What
could be causing the NSTextField delegate methods to stop working after refactoring from NSView to NSViewController?
@IBOutlet weak var customeView: NSView!
var myDetailsViewController: MyDetailsViewController!
var myDetailsView: MyDetailsView!
var isViewController: Bool = true
override func windowDidLoad() {
super.windowDidLoad()
if isViewController {
myDetailsViewController = MyDetailsViewController(nibName: "MyDetailsViewController", bundle: nil)
self.customeView.addSubview(myDetailsViewController.view)
} else {
myDetailsView = MyDetailsView.createFromNib()
self.customeView.addSubview(myDetailsView!)
}
}
override func showWindow(_ sender: Any?) {
super.showWindow(nil)
window?.makeKeyAndOrderFront(nil)
}
override var windowNibName: NSNib.Name? {
return NSNib.Name("MyWindowController")
}}
class MyDetailsViewController: NSViewController {
@IBOutlet weak var textField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
extension MyDetailsViewController: NSTextDelegate {
func controlTextDidChange(_ obj: Notification) {
guard let textField = obj.object as? NSTextField else { return }
print("The value is ----> (MyDetailsViewController) \(textField.stringValue)")
}
}
TextField delegate is set in XIB.
Explore the various UI frameworks available for building app interfaces. Discuss the use cases for different frameworks, share best practices, and get help with specific framework-related questions.
Post
Replies
Boosts
Views
Activity
I want to understand the utility of using AsyncStream when iOS 17 introduced @Observable macro where we can directly observe changes in the value of any variable in the model(& observation tracking can happen even outside SwiftUI view). So if I am observing a continuous stream of values, such as download progress of a file using AsyncStream in a SwiftUI view, the same can be observed in the same SwiftUI view using onChange(of:initial) of download progress (stored as a property in model object). I am looking for benefits, drawbacks, & limitations of both approaches.
Specifically, my question is with regards to AVCam sample code by Apple where they observe few states as follows. This is done in CameraModel class which is attached to SwiftUI view.
// MARK: - Internal state observations
// Set up camera's state observations.
private func observeState() {
Task {
// Await new thumbnails that the media library generates when saving a file.
for await thumbnail in mediaLibrary.thumbnails.compactMap({ $0 }) {
self.thumbnail = thumbnail
}
}
Task {
// Await new capture activity values from the capture service.
for await activity in await captureService.$captureActivity.values {
if activity.willCapture {
// Flash the screen to indicate capture is starting.
flashScreen()
} else {
// Forward the activity to the UI.
captureActivity = activity
}
}
}
Task {
// Await updates to the capabilities that the capture service advertises.
for await capabilities in await captureService.$captureCapabilities.values {
isHDRVideoSupported = capabilities.isHDRSupported
cameraState.isVideoHDRSupported = capabilities.isHDRSupported
}
}
Task {
// Await updates to a person's interaction with the Camera Control HUD.
for await isShowingFullscreenControls in await captureService.$isShowingFullscreenControls.values {
withAnimation {
// Prefer showing a minimized UI when capture controls enter a fullscreen appearance.
prefersMinimizedUI = isShowingFullscreenControls
}
}
}
}
If we see the structure CaptureCapabilities, it is a small structure with two Bool members. These changes could have been directly observed by a SwiftUI view. I wonder if there is a specific advantage or reason to use AsyncStream here & continuously iterate over changes in a for loop.
/// A structure that represents the capture capabilities of `CaptureService` in
/// its current configuration.
struct CaptureCapabilities {
let isLivePhotoCaptureSupported: Bool
let isHDRSupported: Bool
init(isLivePhotoCaptureSupported: Bool = false,
isHDRSupported: Bool = false) {
self.isLivePhotoCaptureSupported = isLivePhotoCaptureSupported
self.isHDRSupported = isHDRSupported
}
static let unknown = CaptureCapabilities()
}
Having written a SwiftUI based app on macOS 15.2 with Xcode 16.2 I built a version for Monterey (which required backing off some modern uses, like #Preview and others). But the SwiftUI layout, positioning and dimensions, didn't use anything new so,
I was surprised that the app did not look exactly the same on Monterey. It was not wildly different, but enough that I had to modify several to frame, padding and font parameters in order to restore my desired appearance.
I'm assuming that over the course of generations of SwiftUI, things change -- that's life, but this was a frustrating process for at least two reasons:
view modifiers can't be conditionally compiled so something like this, for example, isn't possible
RoundedRectangle(cornerRadius: 1)
<if Monterey>
.padding(.horizontal, -2.0)
<else>
.padding(.horizontal, -4.0)
<endif>
.frame(height: 4.0)
previewing views is a challenge. Previewing on Sequoia doesn't show what the views will look like on Monterey. I can build the app on Monterey and use the non-macro #preview mechanism, but in order to do that, and the above, I'm pushed to maintain two versions of my app because the following doesn't compile:
#if swift(>=5.9)
#Preview("Monitor") { MonitorView() }
#else
struct MonitorView_Previews: PreviewProvider {
static var previews: some View {
MonitorView()
}
}
#endif
These are not show-stoppers but I do wonder if I'm missing some mechanism(s) to ease the pain, or if SwiftUI functional changes are documented.
"the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions" ...... it killing me !!!!
I'm trying to achieve a specific UI design in SwiftUI where the bottom section of my List has a different background color than the top section. For example in the Medications portion of the Health app, the "Your Medications" Section has a different background than the top "Log" Section. How do I achieve this?:
Here some example code. I wonder if I am supposed to use two Lists instead. If I use two Lists though and nest it in a ScrollView, the height of the lists needs to be specified. I am working with dynamic content, though so I don't think that is ideal.
class ProtocolMedication {} // Example model
struct HomeView: View {
@Query private var protocolMedications: [ProtocolMedication]
var body: some View {
NavigationStack {
List {
// Upper sections with default background
Section {
Text("Content 1")
} header: {
Text("Log")
}
// Bottom section that needs different background
Section {
ForEach(protocolMedications) { medication in
Text(medication.name)
}
} header: {
Text("Your Medications")
}
}
.listStyle(.insetGrouped)
}
}
}
Suppose there are two buttons in VStack, the second button is unclickable. I'm running macOS 15.2 with Xcode 16.2.
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView(.horizontal) {
VStack {
Spacer()
// this button is clickable
Button("foo") {
print("foo")
}
// this button can't be clicked
Button("bar") {
print("bar")
}
}
}
}
}
If I change .horizontal -> .vertical and VStack -> HStack, the second button behave normally.
If I remove ScrollView, everything works fine.
it works fine before macOS 15.2.
Iām trying to record videos with AvAssetWriter but sometimes my videos exempt audio buffers recorded and when audio is included, the stream of video buffers stops.
the gist below is my code.
https://gist.github.com/kwameaj67/70a3409c84d48cf758b3734c08a46244
I have written a calculator app. Its main window is a UIView subclass that usually receives user input from the touchscreen, tapping on a virtual keyboard (not the standard pop-up keyboard).
I recently added hardware keyboard support. In order to receive key events, I implemented canBecomeFirstResponder and made it always return YES, and I'm calling becomeFirstResponder whenever the main window becomes active, and resignFirstResponder when it becomes inactive.
This is working fine except for one scenario: when running the app on an iPad, together with another app, using Split View or Slide Over, and the other app has keyboard focus on a text field, my app doesn't receive keyboard events, even when it's the foreground app. I have to go into the other app, and tap somewhere outside a text field, and then return to my app, before my app is getting key events again.
If the user taps on an actual text field in my app, it gets focus just fine, of course, but apparently my UIView calling becomeFirstResponder is not enough to take away the focus from the other app.
Is there a way to steal the focus from another app's text field in this scenario?
All,
Thanks in advance! I'm having a very hard time increasing the MTU to any value beyond 20. The research I've done states iOS 16.1 and beyond supports up to 512 bytes. Yet, the peripheral device will only read 20 bytes.
It's to be noted that I'm using Expo SDK 51 Bare Workflow, and the react-native-ble-plx library. I have the app functioning as both Central and Peripheral on iOS 18.1 devices, and data is successfully being written and read to the characteristic.
Because the Expo app is Bare Workflow, I'm able to make any configurations via Xcode, and if there is any patches needed to the react-native-ble-plx library, we have the architecture to support that too. I wanted to provide that context before being recommended to go to the Expo forums (which I have/will be).
I also added the CoreBluetooth framework to the project in hopes that would overwrite the react-native-ble-plx imports, but I noticed react-native-ble-plx uses Swift while CoreBluetooth is Objective-C.
Looking forward to your responses!
I need a really modal alert, i.e., one which runs and allows the user interaction and closes and returns the selected option inside of a single method.
(I need to serve an API which calls my callback, presuming I show an alert, get the user's decision, and return it from that very callback. The callback comes in the main thread.)
What's the proper way to do this in i(Pad)OS 18+? Thanks!
For reference, up to 17, the following code worked nicely; in 18, it does not anymore:
volatile BOOL __block stillRuns=YES;
UIAlertController* ac=[UIAlertController alertControllerWith... preferredStyle:UIAlertControllerStyleAlert];
[ac addAction:[UIAlertAction actionWith... handler:^(UIAlertAction * _Nonnull action) {
stillRuns=NO;
}]];
...
[UIApplication.sharedApplication.keyWindow.rootViewController presentViewController:ac animated:YES completion:nil];
while (stillRuns) [NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.1]];
Several users are seeing a crash that occurs when configureCell is called for a UICollectionView. This seems to occur specifically for cells that add SwiftUI views using UIHostedConfiguration.
0 libsystem_kernel.dylib 0xc1d4 __pthread_kill + 8
1 libsystem_pthread.dylib 0x7ef8 pthread_kill + 268
2 libsystem_c.dylib 0x77ad8 abort + 128
3 libswiftCore.dylib 0x3cc1a4 swift::fatalError(unsigned int, char const*, ...) + 134
4 libswiftCore.dylib 0x3cc1c4 swift::warningv(unsigned int, char const*, char*) + 30
5 libswiftCore.dylib 0x3cc810 swift_beginAccess + 538
6 libswiftCore.dylib 0x3cc864 swift_beginAccess + 84
7 SwiftUI 0x947a0 _UIHostingView._rootView.getter + 92
8 SwiftUI 0x94668 _UIHostingView.makeRootView() + 432
9 SwiftUI 0x943b8 _UIHostingView.updateRootView() + 264
10 SwiftUICore 0xa0d000 closure #1 in ViewRendererHost.updateGraph() + 204
11 SwiftUICore 0x1f1308 partial apply for thunk for @escaping @callee_guaranteed () -> () + 28
12 AttributeGraph 0x144a8 AGGraphWithUpdate + 84
13 SwiftUICore 0x4a5f60 @objc closure #1 in closure #1 in closure #1 in static Update.syncMain(_:) + 84
14 SwiftUICore 0x3620 wait_for_lock + 112
15 SwiftUICore 0x3588 _MovableLockLock + 68
16 SwiftUICore 0x5bb6d4 _ViewList_ReleaseElements.deinit + 64
17 SwiftUICore 0x5bb838 _ViewList_ReleaseElements.__deallocating_deinit + 16
18 libswiftCore.dylib 0x3d783c _swift_release_dealloc + 56
19 libswiftCore.dylib 0x3d8950 bool swift::RefCounts<swift::RefCountBitsT<(swift::RefCountInlinedness)1>>::doDecrementSlow<(swift::PerformDeinit)1>(swift::RefCountBitsT<(swift::RefCountInlinedness)1>, unsigned int) + 160
20 SwiftUICore 0x7cf7bc assignWithTake for _ViewList_View + 52
21 SwiftUICore 0x18dbe0 assignWithTake for ExclusiveGesture + 76
22 SwiftUI 0x13aff8 outlined assign with take of ModifiedContent<TableRowView, CollectionViewCellModifier> + 100
23 SwiftUI 0x1394fc specialized PlatformListCell<>.defaultConfigureContent(_:transaction:) + 296
24 SwiftUI 0xb8308 CollectionViewListDataSource.configureCell(_:transaction:context:) + 1200
25 SwiftUI 0xb8e20 ShadowListDataSource.configureCell(_:transaction:context:) + 1336
26 SwiftUI 0xb670c closure #1 in UICollectionViewListCoordinatorBase.updateListContents(_:) + 452
27 SwiftUI 0xead7b4 partial apply for closure #1 in ListCoreDataSource.visitRowAt<A>(_:visitor:) + 32
28 SwiftUI 0xb6160 partial apply for closure #1 in ListCoreDataSource.visitRowAt<A>(_:visitor:) + 16
29 SwiftUI 0x171e1c Optional.map<A, B>(_:) + 516
30 SwiftUI 0xb98d8 ListCoreDataSource.visitRowAt<A>(_:visitor:) + 1064
31 SwiftUI 0x33b3c ListCoreDataSource.visitContent<A>(atRow:visitor:) + 536
32 SwiftUI 0xb5478 UICollectionViewListCoordinatorBase.updateListContents(_:) + 1696
33 SwiftUI 0x942dac closure #1 in performUpdates #1 <A, B>() in UICollectionViewListCoordinatorBase.update(_:to:transaction:performDiff:) + 416
34 SwiftUI 0x3261cc thunk for @escaping @callee_guaranteed (@unowned Bool) -> () + 48
35 SwiftUI 0x94cf88 closure #1 in UpdateCoalescingCollectionView.performBatchUpdates(_:completion:) + 308
36 SwiftUI 0x3261cc thunk for @escaping @callee_guaranteed (@unowned Bool) -> () + 48
37 UIKitCore 0x2b9bbc -[UICollectionView _updateAnimationDidStop:finished:] + 1240
38 UIKitCore 0xa02c38 __118-[UICollectionView _startViewAnimationsWithContext:oldVisibleViews:viewAnimator:viewAnimationsCompletedDispatchGroup:]_block_invoke + 36
39 UIKitCore 0x7d788 __47-[UICollectionViewAnimation startWithAnimator:]_block_invoke_3 + 272
40 UIKitCore 0x7bfb0 __UIVIEW_IS_EXECUTING_ANIMATION_COMPLETION_BLOCK__ + 36
41 UIKitCore 0x7be94 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 624
42 UIKitCore 0x7abb0 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 436
43 UIKitCore 0x86f38 -[UIViewAnimationState animationDidStop:finished:] + 192
44 QuartzCore 0x4e3cc run_animation_callbacks(void*) + 132
45 libdispatch.dylib 0x40d0 _dispatch_client_callout + 20
46 libdispatch.dylib 0x129e0 _dispatch_main_queue_drain + 980
47 libdispatch.dylib 0x125fc _dispatch_main_queue_callback_4CF + 44
48 CoreFoundation 0x56204 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
49 CoreFoundation 0x53440 __CFRunLoopRun + 1996
50 CoreFoundation 0x52830 CFRunLoopRunSpecific + 588
51 GraphicsServices 0x11c4 GSEventRunModal + 164
52 UIKitCore 0x3d2eb0 -[UIApplication _run] + 816
53 UIKitCore 0x4815b4 UIApplicationMain + 340
The crashes started occurring in October and only for iOS 18 users (all versions from 18.0.0 to 18.2.0).
We have several cell types that use SwiftUI views and I haven't been able to isolate the issue to any particular cell type.
I have tried setting contentConfiguration = nil in prepareForReuse but this didn't help.
Has there been a change in UICollectionView/UIHostingConfiguration in iOS 18? Any ideas on how this can be further investigated or resolved?
private let datePicker = {
let picker = UIDatePicker()
picker.backgroundColor = .clear
picker.datePickerMode = .dateAndTime
picker.preferredDatePickerStyle = .compact
return picker
}()
Hello, and an early "Merry Christmas" to all,
I'm building a SwiftUI app, and one of my Views is a fullscreen UIViewRepresentable (SpriteView) beneath a SwiftUI interface.
Whenever the user interacts with any SwiftUI element, the UIView registers a hit in touchesBegan(). For example, my UIView has logic for pinching (not implemented via UIGestureRecognizer), so whenever the user holds down a SwiftUI element while touching the UIView, that counts as two touches to the UIView which invokes the pinching logic.
Things I've tried to block SwiftUI from passing the gesture down to the UIView:
Adding opaque elements beneath control elements
Adding gestures to the elements above
Adding gesture masks to the gestures above
Converting eligible elements to Buttons (since those seem immune)
Adding SpriteViews beneath those elements to absorb gestures
So far nothing has worked. As long as the UIView is beneath SwiftUI elements, any interactions with those elements will be registered as a hit.
The obvious solution is to track each SwiftUI element's size and coordinates with respect to the UIView's coordinate space, then use exclusion areas, but this is both a pain and expensive, and I find it hard to believe this is the best fix for such a seemingly basic problem.
I'm probably overlooking something basic, so any suggestions will be greatly appreciated
I currently face an Issue where the SafeAreaInsets on the iPhone 16 Pro Max is not respected on LaunchScreens.
Lets say you have an ImageView whose leading, trailing and top are equal to the SafeAreaLayoutGuides leading, trailing and top.
Then you have a SwiftUI View such as the following representing the same layout in code:
GeometryReader { reader in
ZStack {
VStack(spacing: 0) {
Spacer(minLength: 0)
.frame(height: reader.safeAreaInsets.top)
Image(decorative: "splashLogo")
Spacer(minLength: 0)
}
.frame(width: reader.size.width)
}
.edgesIgnoringSafeArea(.all)
}
Both the storyboard preview as well as the SwiftUI preview show identical results in Xcode. Launching the app on the device however briefly shows the image below the Dynamic Island cutout until the app is launched to the SwiftUI view. Noticed this only happening on the iPhone 16 Pro Max.
Hello everyone,
I've been having a bit of trouble with the .symbolRenderingMode(_:) modifier.
When trying to apply it to a single button in a toolbar, it does not work at all. The symbol is always rendered as monochrome.
However, I've realised that with this little hack I can achieve the expected results, but this is not ideal.
.toolbar {
HStack {
Button("", action: {}) // The hack
Button("Button", systemImage: "line.3.horizontal.decrease.circle.fill", action: {})
.symbolRenderingMode(.hierarchical)
.foregroundStyle(.blue)
}
}
I've submitted a bug report (FB16129223) but in the meantime, is this my only solution ?
Side note: the foregroundStyle(_:) modifier is ignored as well.
Hello. I am developing an application using Swift 6 and SwiftUI.
I have custom implemented a BottomSheet that animates from bottom to top, and I attempted to achieve this animation by changing the alignmentGuide like this.
ZStack(alignment: .bottom) {
dimView
.opacity(isVisible ? 1 : 0)
.transaction { transaction in
transaction.animation = .easeInOut(duration: 0.35)
}
bottomSheetView
.alignmentGuide(VerticalAlignment.bottom) { dimension in
// compile error occur because isVisible property is state of MainActor isolated View!
isVisible ? dimension[.bottom] : dimension[.top]
}
}
There were no issues in Swift 5, but now I am encountering compile errors because the computeValue closure of the alignmentGuide is not isolated to the MainActor, preventing me from calling view state values or functions.
So I am curious if there are any plans to isolate this closure to the MainActor. From my observation, this closure is always called on the main thread.
Thank you.
It happens on a brand new SwiftUI Xcode project with nothing in it but the default base code. When I build and run, I get "Error creating the CFMessagePort needed to communicate with PPT." After some Googling, I know it has something to do with Performance Tools. I don't know what's causing it.
I'm running 16.2 on a brand new M4 Mac mini.
I have an issue where a very specific configuration of .overlay, withAnimation, and a bindable state can freeze the app when the state changes.
I've isolated the problematic source code into a sample project can be found here that demonstrates the issue:
https://github.com/katagaki/IcyOverlay
Steps to Reproduce
To reproduce the issue, tap the 'Simulate Content Load' button.
Once the progress bar has completed, a switch is toggled to hide the progress view, which causes the overlay to disappear, and the app to freeze.
Any help and/or advice will be appreciated!
Development Environment
Xcode Version 16.2 (16C5032a), macOS 15.2ļ¼24C101ļ¼
iOS SDK: 18.2 (22C146), Simulator: 18.2 (22C150)
I am very new to all this. What I wanted:
I wanted this textfield (where there is "I am the placeholder") to allow users to type any string:
At the moment this textfield does not let me type in anything. It sits like a stone. There are no corresponding errors to this but this behaviour is not my liking. I wanted it to let me just type string.
Could anyone kindly point me in the right direction?
This is how IBOutlet connection made to the TextField in question
And the code snippet
class ViewController: UIViewController {
@IBOutlet weak var companyTextField2: UITextField!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
private var models = [Consulting]()
//more other codes
@IBAction func insertName(_ sender: UITextField) {
// Create the UITextField
let nameTextField = UITextField()
nameTextField.placeholder = "Enter Name"
// Option 1: Add the UITextField to the view (if needed)
view.addSubview(nameTextField)
// Option 2: Use the UITextField (e.g., get or set its text)
let enteredName = nameTextField.text
print("Entered Name:", enteredName ?? "")
}
}
NavigationTitle does not change when the app language changes. It works well in iOS 17.5 but does not in iOS 18.x