Construct and manage graphical, event-driven user interfaces for iOS or tvOS apps using UIKit.

UIKit Documentation

Post

Replies

Boosts

Views

Activity

drawHierarchy speed
In iOS, I'm trying to do multiple (upwards of 10) screen snapshots (UIImages) per second to be combined into a video and am running into some challenges. UIView's drawHierarchy seems to be to slow when run on the main thread, causing jitter with the UI experience. I've noticed that I can run it on a background thread, but only when passing a value of FALSE for the afterScreenUpdates parameter. This does cause the main thread warning to be written to the console (when the main thread checker is turned off), but does work. And I don't have to worry about blocking the main thread as it is on a background thread. The question is, will running drawHierarchy on the background thread potentially cause memory corruption when traversing the entire tree of a UIWindow. And if so, what is the advised most performant method of capturing iOS screenshots? drawHierarchy Doc: https://developer.apple.com/documentation/uikit/uiview/1622589-drawhierarchy TIA
4
0
172
4w
iOS 18 developer beta: Writing Tools
Based on the session content, it seems that setting the TextView property writingToolsBehavior = .complete should bring up the writing tools bottom panel view. However, it does not appear to be working. Is this a feature that will be added in a future update, or is there something additional I need to do? Test on: XCode 16.0 beta (16A5171c), iOS Simulator 18.0 Beta, iPhone 11 Pro iOS 18.0 Beta
4
6
379
Jun ’24
Why is KeyboardLayoutGuide not applied in viewIsAppearing() in iOS 15.0?
viewIsAppearing is available in iOS 13 and above, and I understand that setting the layout in this lifecycle is a good way to do it. But when I use it, confirmed that UIKeyboardLayout does not work properly in viewIsAppearing in iOS versions lower than 16.0. There is a small problem not only with viewIsAppearing, but also with viewDidLoad(). In viewDidLoad(), the component to which UIKeyboardLayoutGuide is applied is visible when the screen first appears. However, there is a problem where the layout with UIKeyboardLayoutGuide applied is not visible when returning after changing the navigation screen (Push & Pop). I confirmed that UIKeyboardLayoutGuide is applied properly in viewIsAppearing and viewDidLoad in iOS 16.0 and higher versions. Below is the code and environment for reproducing the problem situation. ==================================================== Envirionment: M1 Pro(Sonoma v14.5), Xcode(v15.4), iPhone Simulator (iOS v15.0, v15.2) import UIKit import SnapKit final class ViewController: UIViewController { private let uiTextField: UITextField = { $0.placeholder = "search" $0.backgroundColor = .red return $0 }(UITextField()) override func viewIsAppearing(_ animated: Bool) { super.viewIsAppearing(animated) view.backgroundColor = .white layout() } private func layout() { view.addSubview(uiTextField) uiTextField.snp.makeConstraints { $0.height.equalTo(50) $0.horizontalEdges.equalToSuperview() $0.bottom.equalTo(view.keyboardLayoutGuide.snp.top) } } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) } } I wonder if I haven't checked what I need to check yet, or if there are other problems.
0
0
142
4w
How to close DatePicker after selection in View?
I have a DatePicker in View not Viewcontroller. The solution in stack overflow works fine, if the DatePicker is in ViewController. (https://stackoverflow.com/questions/67109078/close-uidatepicker-after-selection-when-style-is-compact) I want to close DatePIcker after selecting a date. Thank you.
1
0
123
4w
iOS 17 - New status bar default style doesn't work if the status bar was previously hidden
Hi, I adopted in my app the new default style for the status bar where it switches between light and dark themes depending on the color of the content beneath. It works great until I hide the status bar and show it again. From that point onwards, the status bar will be stuck on the dark or light theme, depending on the app's theme. This happens as well if the status bar is initially hidden on startup. In that case, the default behavior never works. I filed an issue (FB13222217), and as of now I didn't find a workaround except never hiding the status bar on my app. I attached a super minimalist example project there, just a scroll view with black and white views and nothing more. This happens on SwiftUI and UIKit as well. I hope the issue can be addressed and will appreciate if anyone has a workaround for this to let me know. Thanks!
3
4
2.4k
Oct ’23
Specify queue for collapse/expand of section snapshots
Is there any way to use outline disclosure sections in a UICollectionViewDiffableDataSource without having to make all apply(_ snapshot:) calls from the main thread? Whenever I collapse or expand a section I get a warning about mixing main queue/off-main queue updates: Warning: applying updates in a non-thread confined manner is dangerous and can lead to deadlocks. Please always submit updates either always on the main queue or always off the main queue I understand what this means, but I am applying my section snapshots on a background queue to avoid interrupting the UI, it seems like the expand/collapse updates use the main queue and I cannot find any way to make it use the same queue as my other updates. Does this mean I must apply my own updates on the main queue now as well?
2
0
164
Jun ’24
UICollectionView has weird separator insets in landscape mode
Here is a screenshot of the app: And here follows the code: it's a view controller with a collection view with plain list layout and a diffable data source. It has 1 section with 2 rows. The 1st row's content configuration is UIListContentConfiguration.cell(), and it has some text. The 2nd row has an empty content configuration instead. As you can see, if you run the app, the separator insets are different for the two rows. Did I make a mistake? If this is expected behavior instead, is there an easy fix? import UIKit class ViewController: UIViewController { var collectionView: UICollectionView! var dataSource: UICollectionViewDiffableDataSource<String, String>! var snapshot: NSDiffableDataSourceSnapshot<String, String> { var snapshot = NSDiffableDataSourceSnapshot<String, String>() snapshot.appendSections(["main"]) snapshot.appendItems(["one", "two"]) return snapshot } override func viewDidLoad() { super.viewDidLoad() configureHierarchy() configureDataSource() } func configureHierarchy() { collectionView = .init( frame: view.bounds, collectionViewLayout: createLayout() ) view.addSubview(collectionView) collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] } func createLayout() -> UICollectionViewLayout { return UICollectionViewCompositionalLayout { section, layoutEnvironment in let config = UICollectionLayoutListConfiguration(appearance: .plain) return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment) } } func configureDataSource() { let firstCellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String> { cell, indexPath, itemIdentifier in var contentConfiguration = UIListContentConfiguration.cell() contentConfiguration.text = "Hello" cell.contentConfiguration = contentConfiguration } let secondCellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String> { cell, indexPath, itemIdentifier in } let emptyCellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String> { cell, indexPath, itemIdentifier in } dataSource = .init(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in switch itemIdentifier { case "one": collectionView.dequeueConfiguredReusableCell(using: firstCellRegistration, for: indexPath, item: itemIdentifier) case "two": collectionView.dequeueConfiguredReusableCell(using: secondCellRegistration, for: indexPath, item: itemIdentifier) default: collectionView.dequeueConfiguredReusableCell(using: emptyCellRegistration, for: indexPath, item: itemIdentifier) } } dataSource.apply(self.snapshot, animatingDifferences: false) } } Xcode 15.4, iPhone 15 Pro simulator, iOS 17.5, MacBook Air M1 8GB, macOS 14.5.
1
0
178
Jun ’24
PKCanvasView won't react to updates of its contentScaleFactor
Dear All, I am currently facing a challenge with updating the contentScaleFactor of my PKCanvasView, which is embedded within a custom UIScrollView. I have configured the PKCanvasView to have both its maximum and minimum zoomScale set to 1.0, ensuring that users can only zoom into the UIScrollView and not directly into the PKCanvasView. Upon the completion of a zoom action in my UIScrollView, a notification is published and received by the PKCanvasView. This triggers a function intended to update the contentScaleFactor. However, this process is not functioning as expected. Despite consulting with several engineers at WWDC24 and exploring multiple potential solutions, the issue remains unresolved. Interestingly, when zooming into a PDF in the Documents app on iOS 17, the strokes are re-rendered perfectly, while in previous iOS versions, this bug persists in the Documents app. I would greatly appreciate any insights or solutions from those who might have encountered and resolved this issue. Thank you in advance for your assistance. Best regards, Timon
0
1
193
Jun ’24
UIPasteControl sometimes disappear
Problem Our app use UIPasteControl for people taps to place pasteboard contents in UITextView. It worked fine at first, but recently received a lot of user feedback and the button suddenly disappeared This problem usually occurs when an App switches between the front and back More Information When the button disappears, we find that the child view of the UIPasteControl control which name _UISlotView has a size of zero. we use UIKit and AutoLayout,limit button size (100, 36) let config = UIPasteControl.Configuration() config.displayMode = .labelOnly config.cornerStyle = .fixed config.baseForegroundColor = .white config.baseBackgroundColor = .black config.cornerRadius = 18 let btn = UIPasteControl(configuration: config) pasteBtn = btn addSubview(pasteBtn) pasteBtn.snp.makeConstraints { make in make.trailing.equalTo(-20) make.bottom.equalTo(-10) make.size.equalTo(CGSize(width: 100, height: 36)) } UI view information <UIPasteControl: 0x107dda810; frame = (0 0; 100 36); layer = <CALayer: 0x3010ff000>> (lldb) po [0x107dda810 subviews] <__NSSingleObjectArrayI 0x30152ff00>( <_UISlotView: 0x107dea630; frame = (0 0; 100 36); userInteractionEnabled = NO; layer = <CALayer: 0x3010eb460>> ) anyone meet before? is there a workaround?
2
0
201
Jun ’24
Voice over reads elements for covered UIWindow
I have few UIWindow in my application, one has alert level and another one has normal. When I present one window above another the voice over reads elements that are covered by the alert window. How I can suppress this behaviour and make the voice over read elements on a visible key window only. I don't like an option to hide accessibility for the bottom window, so I am wondering are there any other ways to do it?
1
0
101
Jun ’24
UIPrintInteractionController.shared on Mac OS Sonoma running iPad App
After updating to Mac OS Sonoma, we have encountered compatibility issues with our iPad-designed application, specifically with the AirPrint functionality, when it is run on MacOS. The AirPrint feature stopped working properly through UIPrintInteractionController.shared. We have noticed that when we compile the application using Catalyst, the AirPrint functionality is restored and works as expected. However, this solution is not viable for us due to the restrictions associated with the frameworks we are utilizing. We are seeking alternative solutions, and any help or guidance would be highly appreciated to resolve this issue and ensure a seamless and uninterrupted user experience in our application. STEPS TO REPRODUCE Create an app for ipad with just a button and this code var str = "TEST" let printInfo = UIPrintInfo(dictionary:nil) printInfo.outputType = .general printInfo.jobName = "Report" printInfo.orientation = .portrait let printController = UIPrintInteractionController.shared printController.printInfo = printInfo printController.showsNumberOfCopies = false printController.showsPageRange = false printController.showsNumberOfCopies = false let formatter = UIMarkupTextPrintFormatter(markupText: str) formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72) printController.printFormatter = formatter printController.present(animated: true, completionHandler: nil) 2.Run it on a MacOS with Sonoma, there is no error on console or anything but it don't work. -If you run it with Catalyst it just works when adding the Printing permission of App Sandbox in Signing & Capabilities.
9
0
1.7k
Oct ’23
When using UIView.drawHierarchy, older CPU performs far better than newer CPU
Hi all, My app uses SpriteKit views which are rendered into images for various uses. For some reason, the same code performs worse on a newer CPU than on an older one. My A13 Bionic flies through the task at high resolution and 60FPS while CPU usage is <60%, while the A15 Bionic chokes and sputters at a lower resolution and 30FPS. Because of how counterintuitive this is, it took me a while to isolate the call directly responsible--with UIView.drawHierarchy commented out, both devices returned to their baseline performances. guard let sceneView = skScene.view else { return } let size = CGSize(width: outputResolution, height: outputResolution) return UIGraphicsImageRenderer(size: size).image { context in let rect = CGRect(origin: .zero, size: size) sceneView.drawHierarchy(in: rect, afterScreenUpdates: false) } Does anyone know why this is the case, and how to fix it? I tried using UIView.snapshotView, which is supposedly much faster, but it only returns blank images. Am I using it wrong or does it simply not work in this context? sceneView.snapshotView(afterScreenUpdates: false)?.draw(rect) Any hints or pointers would be greatly appreciated
0
0
173
Jun ’24
Complex Scroll Interaction Issue with three scrolls: horizontal UIPageViewController, vertical UIScrollView, and horizontal UIScrollView on the top of them
Hello Apple Developers, Here is an interesting and quite challenging problem. It is a real problem in our product Drinkit (https://apps.apple.com/app/id1495622004) that we can't solve elegantly. There are three scrolls: a horizontal UIPageViewController, inside each page there is a vertical and paginated UIScrollView, and on top of all this, there is a horizontal UIScrollView with tabs to manage the UIPageViewController. We want to make the vertical scroll on these tabs forward to the view controller on the current page of the UIPageViewController so that the page in the vertical UIScrollView scrolls accordingly. I have created a prototype to reproduce this situation. https://github.com/zsergey/Scrollviews Thank you for your help!
0
0
87
Jun ’24
The application crashes when an image of a certain PDF (50 MB, 1 page) is drawn on the context page in background.
Hi Team, I am using CGPDFPage and CGContext to convert the PDF to an image. It is working fine in all circumstances but fails in one circumstance where the size of the PDF is above 50 MB and when the application is made to be on background and again opened. It is also occurring only on physical devices; emulators are working fine. We are using the following code for this conversation: fileStream.CopyTo(memoryStream); CGDataProvider provider = new CGDataProvider(memoryStream.ToArray()); CGPDFDocument cGPDFDocument = null; image = null; cGPDFDocument = new CGPDFDocument(provider); using(CGPDFPage pdfPage = cGPDFDocument?.GetPage(1)) { if (pdfPage != null) { CGRect rect = pdfPage.GetBoxRect(CGPDFBox.Media); if (pdfPage.RotationAngle == 90 || pdfPage.RotationAngle == 270) { rect = new CGRect(rect.X, rect.Y, rect.Height, rect.Width); } nfloat factor = (nfloat)0.5; CGRect bounds = new CGRect(rect.X * factor, rect.Y * factor, rect.Width * factor, rect.Height * factor); UIGraphics.BeginImageContext(bounds.Size); CGContext context = UIGraphics.GetCurrentContext(); context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f); context.FillRect(bounds); context.TranslateCTM(0, bounds.Height); context.ScaleCTM(factor, -factor); context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true)); context.SetRenderingIntent(CGColorRenderingIntent.Default); context.InterpolationQuality = CGInterpolationQuality.Default; context.DrawPDFPage(pdfPage); image = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); imageView.Image = image; parentView.AddSubview(imageView); } } We have added a simple application created on Xamarin.iOS platform that replicates this issue in Assets we have added the PDF with this issue https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfToImageSample-556012354 We have recorded this for the reference, https://www.syncfusion.com/downloads/support/directtrac/general/ze/CrashBGImgRec-1376481150 This issue is constantly occurring on the "context.DrawPDFPage(pdfPage);" line in my application. On the provided sample, this issue is occurring consistently when the break point is placed on the "context.DrawPDFPage(pdfPage);" line. It also occurs randomly without placing the breakpoint. We are invoking this function on DidEnterBackground override method of AppDelegate Do we need to set any to properly retrieve the image from CGPDFPage and add them into context?
0
0
144
Jun ’24
Crashed iOS app at CA::Transaction::commit()
Our on field app is facing significant crashes at CA::Transaction::commit() from QuartzCore. The crash report does not point to any code in our app and the information available is very limited. It's a EXC_BAD_ACCESS KERN_INVALID_ADDRESS crash. We have tried using sanitisers and zombies but unable to reproduce the crash locally. Can someone help explain and point in right direction? Below are the crash details. Full crash report Distributor ID: com.apple.AppStore Hardware Model: iPhone15,3 AppStoreTools: 15F31e AppVariant: 1:iPhone15,3:16 Code Type: ARM-64 (Native) Role: Foreground Parent Process: launchd [1] Date/Time: 2024-06-03 20:41:59.3315 +0530 Launch Time: 2024-06-03 19:15:20.6051 +0530 OS Version: iPhone OS 17.4.1 (21E236) Release Type: User Baseband Version: 2.51.04 Report Version: 104 Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000000000110 Exception Codes: 0x0000000000000001, 0x0000000000000110 VM Region Info: 0x110 is not in any region. Bytes before following region: 4372119280 REGION TYPE START - END [ VSIZE] PRT/MAX SHRMOD REGION DETAIL UNUSED SPACE AT START ---> __TEXT 104994000-105420000 [ 10.5M] r-x/r-x SM=COW /var/containers/Bundle/Application/ Termination Reason: SIGNAL 11 Segmentation fault: 11 Terminating Process: exc handler [35195] Triggered by Thread: 0 Kernel Triage: VM - (arg = 0x3) mach_vm_allocate_kernel failed within call to vm_map_enter VM - (arg = 0x3) mach_vm_allocate_kernel failed within call to vm_map_enter VM - (arg = 0x3) mach_vm_allocate_kernel failed within call to vm_map_enter VM - (arg = 0x3) mach_vm_allocate_kernel failed within call to vm_map_enter Thread 0 name: Thread 0 Crashed: 0 QuartzCore 0x000000018f4633b4 CA::Transaction::commit() + 1152 (CATransactionInternal.mm:480) 1 QuartzCore 0x000000018f4633ec CA::Transaction::commit() + 1208 (CATransactionInternal.mm:480) 2 QuartzCore 0x000000018f462e64 CA::Transaction::flush_as_runloop_observer(bool) + 88 (CATransactionInternal.mm:942) 3 UIKitCore 0x00000001900b1260 _UIApplicationFlushCATransaction + 52 (UIApplication.m:3160) 4 UIKitCore 0x00000001900b0d78 _UIUpdateSequenceRun + 84 (_UIUpdateSequence.mm:119) 5 UIKitCore 0x00000001900b0468 schedulerStepScheduledMainSection + 144 (_UIUpdateScheduler.m:1037) 6 UIKitCore 0x00000001900b0524 runloopSourceCallback + 92 (_UIUpdateScheduler.m:1186) 7 CoreFoundation 0x000000018ddc162c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28 (CFRunLoop.c:1957) 8 CoreFoundation 0x000000018ddc08a8 __CFRunLoopDoSource0 + 176 (CFRunLoop.c:2001) 9 CoreFoundation 0x000000018ddbf0b8 __CFRunLoopDoSources0 + 340 (CFRunLoop.c:2046) 10 CoreFoundation 0x000000018ddbdd88 __CFRunLoopRun + 828 (CFRunLoop.c:2955) 11 CoreFoundation 0x000000018ddbd968 CFRunLoopRunSpecific + 608 (CFRunLoop.c:3420) 12 GraphicsServices 0x00000001d20b34e0 GSEventRunModal + 164 (GSEvent.c:2196) 13 UIKitCore 0x0000000190230edc -[UIApplication _run] + 888 (UIApplication.m:3692) 14 UIKitCore 0x0000000190230518 UIApplicationMain + 340 (UIApplication.m:5282) 15 AppName 0x000000010542072c main + 64 (AppDelegate.swift:13) 16 dyld 0x00000001b12ded84 start + 2240 (dyldMain.cpp:1298)
0
0
172
Jun ’24
UIView can't handle external keyboard shortcuts combined with Command key (UIKeyCommand)
Keyboard shortcuts that use the Command key modifier are not handled properly, and the UIKeyCommand action and pressesBegan and pressesEnded methods are not called at all or are called unreliably. It is easy to reproduce using this snippet: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let textView = MyTextView() textView.font = UIFont.systemFont(ofSize: 24) textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec efficitur eros vitae dui consectetur molestie. Integer sed massa rutrum, pharetra orci eget, molestie sem. Fusce vestibulum massa nisi, vitae viverra purus condimentum et. Sed nec turpis aliquam, tempus enim sit amet, gravida libero. Praesent scelerisque venenatis nunc, vel convallis nisl auctor vitae. Mauris malesuada tempus pharetra. Nullam ornare venenatis ullamcorper. In viverra feugiat tincidunt. Nullam iaculis urna eu semper rutrum. " textView.isEditable = true textView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(textView) NSLayoutConstraint.activate([ textView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), textView.bottomAnchor.constraint(equalTo: view.bottomAnchor), textView.leadingAnchor.constraint(equalTo: view.leadingAnchor), textView.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) } } class MyTextView: UITextView { override var keyCommands: [UIKeyCommand]? { [ UIKeyCommand(input: "[", modifierFlags: .command, action: #selector(commandAction(_:))) ] } override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) { print("pressesBegan") super.pressesBegan(presses, with: event) } override func pressesEnded(_ presses: Set<UIPress>, with event: UIPressesEvent?) { print("pressesEnded") super.pressesEnded(presses, with: event) } @objc private func commandAction(_ sender: Any?) { print("commandAction") } } Run the code in a Simulator or on a Device with an external keyboard connected. Observe the console for a string "commandAction" when pressing the combination Command + [ on the keyboard. Result it not predictable, the UIKeyCommand is not called at all, or called in a loop, or sometimes called after change selection in the UITextView. The same with pressesBegan and pressesEnded. Compare results with the change where instead of Command modifier, we use Control modifier eg.: "UIKeyCommand(input: "[", modifierFlags: .command, action: #selector(commandAction(_:))" - now each keyboard shortcut is properly reported to methods. The UIKeyCommand.wantsPriorityOverSystemBehavior property changes nothing. Behavior reproducible in the Simulator and on the Device (iPad) (the issue was confirmed during online WWDC24 Labs) Reported as FB13897415
0
0
219
Jun ’24
UIFont crash on iOS 18
We found a new crash about UIFont on iOS 18 beta, and this crash never occurred on pervious operating system. Can anyone know how to fix it and whether apple will fix it on later new beta version?
2
1
239
Jun ’24
Swift 6 actor error in didReceiveRemoteNotification of UIApplicationDelegate
From Xcode 16.0 Beta I am getting the following error at didReceiveRemoteNotification of UIApplicationDelegate protocol: Non-sendable type '[AnyHashable : Any]' in parameter of the protocol requirement satisfied by main actor-isolated instance method 'application(_:didReceiveRemoteNotification:)' cannot cross actor boundary; this is an error in the Swift 6 language mode Generic struct 'Dictionary' does not conform to the 'Sendable' protocol (Swift.Dictionary) How can I fix this warning before moving to Swift 6 mode.
0
1
148
Jun ’24