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

UIKit Documentation

Post

Replies

Boosts

Views

Activity

Reconfigure UICollectionView section
Problem When I reconfigure a collection view snapshot and apply it to my complex production collection view with list layout, the UI updates slowly, unless I don't animate the differences, even though I'm only reconfiguring the specific item identifiers of the cells that should be updated. I thought that maybe I could speed up the animations of the UI updates by applying the snapshot to a specific section (dataSource?.apply(sectionSnapshot, to: .main)) instead of to the whole collection view (dataSource?.apply(wholeSnapshot)). The problem is that I can't reconfigure the item identifiers of a single section snapshot to then apply the updated snapshot. Question Given the following sample app, can anybody edit reconfigureMainSection() so that, when invoked, the .main section cell registrations are called, consequently invoking print("Reconfiguring")? Sample app This collection view with list layout and diffable data source has 1 section and 2 rows. You can tap the right bar button item to call reconfigureMainSection(). class ViewController: UICollectionViewController { var snapshot: NSDiffableDataSourceSnapshot<Section, String> { var snapshot = NSDiffableDataSourceSnapshot<Section, String>() snapshot.appendSections([.main]) snapshot.appendItems(["one", "two"], toSection: .main) return snapshot } var dataSource: UICollectionViewDiffableDataSource<Section, String>? enum Section { case main } init() { super.init(collectionViewLayout: .init()) collectionView.collectionViewLayout = createLayout() configureDataSource() } override func viewDidLoad() { super.viewDidLoad() navigationItem.rightBarButtonItem = .init( title: "Reconfigure", style: .plain, target: self, action: #selector(reconfigureMainSection) ) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func configureDataSource() { let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String> { cell, indexPath, itemIdentifier in print("Reconfiguring") } dataSource = .init(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier) } dataSource?.apply(self.snapshot, animatingDifferences: false) } func createLayout() -> UICollectionViewLayout { return UICollectionViewCompositionalLayout { section, layoutEnvironment in let config = UICollectionLayoutListConfiguration(appearance: .plain) return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment) } } @objc func reconfigureMainSection() { var sectionSnapshot = NSDiffableDataSourceSectionSnapshot<String>() sectionSnapshot.append(snapshot.itemIdentifiers(inSection: .main)) // reconfigure the section snapshot dataSource?.apply(sectionSnapshot, to: .main) } } Environment MacBook Air M1 8GB macOS Sonoma 14.5 Xcode 15.4 iPhone 15 Pro simulator on iOS 17.5
3
0
291
Aug ’24
Xcode16 openURL is invalid
Xcode version: Xcode16 beta6 iOS version:iOS18 beta7 The openURL method call to UIApplication is invalid Moreover, the project has a lot of logic dependent on the return value of this method, which is very troublesome to modify Look at the Apple documentation, and say that the problem is fixed, how should I deal with this situation
1
0
662
Aug ’24
iOS 18 Beta UIPrintInteractionController present issue
I have a UIViewController that presents a UIPrintInteractionController when a user selects the print button on the UI. The problem is starting in iOS 18 (currently using beta 7) when the print controller is presented the UIViewController's viewWillAppear() is being called. This did not happen in earlier iOS releases and is causing unwanted behavior in the app. Is this a bug or will this be the behavior going forward?
2
0
505
Aug ’24
iOS 18 Navigation Title Bug
Hello. I'm encountering a strange behavior in iOS 18 and wanted to ask about it. When I run the following code on iOS 18, quotation marks appear in the navigation title. class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.title = " " self.view.backgroundColor = .systemBlue } } In versions prior to iOS 18, the title is displayed as intended, with nothing showing up. Could this be an intentional change in iOS 18, or is it a bug?
3
1
900
Aug ’24
Issues with persistent SwiftUI.AppSceneDelegate causing black screen on app launch
For a few reasons, we as a team decided to transition away from the SwiftUI.App launch cycle. We need more control and customisation, and managing the scenes directly ourselves was the best way forward. We're now trying to run the app entirely from our own App and Scene delegates, however we've run into a problem: When we update the app on a device from a version that uses SwiftUI.App to a version that uses App and Scene delegates, the first initial launch fails with a black screen. The reason for this is that FrontBoardServices attempts to use the existing UISceneSession after the update, with the SwiftUI.AppSceneDelegate class, instead of calling application(configurationForConnecting:options:). Since SwiftUI.AppSceneDelegate still exists in the scope, the internal property _configurationNeedsReevaluation on UISceneSession returns false and the app attempts to launch from SwiftUI.AppSceneDelegate. As far as I can tell, there doesn't seem to be a way to fix this that doesn't involve invoking the private method -[UISceneSession _updateConfiguration:] to force the configuration to use the correct Scene delegate. if let sceneSession = application.openSessions.first( where: { $0.configuration.delegateClass.map(NSStringFromClass) == "SwiftUI.AppSceneDelegate" } ) { let newConfig = UISceneConfiguration(name: nil, sessionRole: .windowApplication) newConfig.delegateClass = SceneDelegate.self sceneSession.perform(Selector(("_updateConfiguration:")), with: newConfig) } Could you maybe advise on a way forwards?
2
0
359
Aug ’24
UlTextView erroneously overrides string attributes when applying spellchecker annotation attributes (regression)
UITextView erroneously overrides string attributes when applying spellchecker annotation attributes. It doesn't need any particular setting. Default UITextView instance with attributed text let textView = UITextView(usingTextLayoutManager: true) textView.spellCheckingType = .yes Once spellcheck attributes get applied, other attributes like foreground color get applied to the misspelled words. This behavior happens only on Mac Catalyst, and started to appear on macOS 14 or newer. Please check the Xcode project that demonstrates the issue https://github.com/user-attachments/files/16689336/TextEditor-FB14165227.zip Open TextEditor project Select "My Mac (Mac Catalyst)" build destination Run the project. A window with a text area should appear Select the whole text (either using mouse or keyboard command+a) Observe how foregroundColor changes to text (this is the issue) That eventually led to crash 💥 This bug is reported to Apple FB14165227
1
0
476
Aug ’24
UITableView: Adjust destination index path when dragging and dropping from another view
When reordering items within a table view, I can use tableView:targetIndexPathForMoveFromRowAtIndexPath: to adjust the destination index path. Is there a way to adjust the destination index path when dropping onto a table view if the drag was initiated in a different view? For example, if I'm dropping an object into a table view where the rows are sorted alphabetically, I want the gap to appear in the location where the item is going to end up, regardless of which row the dragged item is being dragged / dropped at.
1
0
253
Aug ’24
PullDown menu shows in reverse order when in landscape
I create a UIKit PullDown menu (in a positionMenu button), with the following code: func setupPullDownMenu() { let menu1 = UIAction(title: "Menu1") { [self] _ in // some code } let menu2 = UIAction(title: "Menu2") { [self] _ in // some code } let menu3 = UIAction(title: "Menu3") { [self] _ in // some code } let menu = UIMenu(title: "Positions", children: [menu1, menu2, menu3]) positionMenu.menu = menu positionMenu.showsMenuAsPrimaryAction = true }   In Portrait, options are listed as Menu1, Menu2, Menu3 But in Landscape it is the reverse: Menu3, Menu2, Menu1 I use Xcode 16.1ß and iOS 17.0 simulator Is it the expected behaviour ?
1
0
403
Aug ’24
UIDeferredMenuElement on MacCatalyst: closure called only once on first menu display
Hi folks, I'm struggling with an issue on MacCatalyst. I'm using a dynamic menu that is supposed to show the current state of a setting (state = .on / .off). The UIDeferredMenuElement works great on iOS, the closure is called on each display of the (context) menu. On MacCatalyst however - where I use the menu as a submenu in the main menu - the closure is called only once on first menu display. The system seems to show a cached menu item even though I'm using UIDeferredMenuElement.uncached. Here's the relevant code: func getMenuElement() { let element = UIDeferredMenuElement.uncached { completion in let modeString = mode.myLocalizedDescription let trackingMode = <current setting> let mode = <menu item setting option> let state = trackingMode == mode ? .on : .off let action = UIAction(title: modeString, image: <image>, state: state) { _ in viewController.setUserTrackingMode(mode) } completion([action]) } return element } Any ideas how to trick the system into generating the dynamic menu item on each menu display?
1
0
307
Aug ’24
Critical Issue in iOS 18 Beta: UITabBarController Child View Controller Incorrectly Added as UITabBarItem, Leading to Application Crash
I am writing to report an issue I encountered with iOS 18 beta that affects my application, which has been available on the App Store for over two years and currently has over 60,000 active users. My application utilizes a UITabBarController to manage multiple tabs, where each tab hosts a UIViewController embedded within a UINavigationController. The application operates in two different states, where users may have either 5, 4, or 3 tabBarItems depending on their configuration. The issue arises when fewer than 5 tabs are present. In these cases, I add child view controllers to the UITabBarController to ensure they are displayed above the tab bar, rather than below it. The relevant code snippet is as follows: tabBarController.addChild(childController) tabBarController.view.addSubview(childController.view) Prior to iOS 18, this implementation functioned as expected. However, with the release of iOS 18, adding a child view controller to the UITabBarController results in the child being incorrectly added as a UITabBarItem. This misbehavior leads to an application crash when the unintended tab is selected. The crash trace is as follows: "Inconsistency in UITabBar items and view controllers detected. No view controller matches the UITabBarItem '<UITabBarItem: 0x142d9c480> selected'." I have attached screenshots from iOS 18 and previous versions to illustrate the issue, which compares the expected behavior in earlier iOS versions with the problematic behavior in iOS 18. I appreciate your attention to this matter and look forward to any guidance or resolution you can provide.
3
0
1.3k
Aug ’24
Which scene is activated when a file is shared to my app?
I'd like to receive files in my app, but only certain UIScenes can receive a file. How do I tell the system which of the UIScenes in my app can receive that file? How does the system pick a scene? I've tried using UISceneActivationConditions(). Here is what I put in the scenes that can receive files: var preferredActivatePredicates = [NSPredicate]() var canActivatePredicates = [NSPredicate]() // Any scene *can* be activated at any time canActivatePredicates.append(NSPredicate(value: true)) if hasWriteAccess { // We are also the preferrred scene for receiving files for fileExtension in ["png", "jpeg", "jpg", "pdf"] { preferredActivatePredicates.append(NSPredicate(format: "SELF ENDSWITH[c] '.%@'", fileExtension)) } } let conditions = UISceneActivationConditions() conditions.canActivateForTargetContentIdentifierPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: canActivatePredicates) conditions.prefersToActivateForTargetContentIdentifierPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: preferredActivatePredicates) scene.activationConditions = conditions Unfortunately, this does not seem to have an effect. It does not activate the hasWriteAccess scenes for func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) Is there any way to tell what content identifier the system is using when a file share activates an app? Thank you for your help!
1
0
245
Aug ’24
Why is the GameController framework loaded
The question is: We did not add "GameController framework", but we do not know why the "GameController framework" is loaded at startup. I am checking the launch time of our app, using Instruments->App launch I was confused to find the GameConroller framework loaded I check the project, in the plist file, no configuration GCSupportedGameControllers, GCSupportsControllerUserInteraction related key. What else causes the GameController framework to load?
0
0
322
Aug ’24
Subclass UITextView using TextKit2
Instead of implementing a textview from scratch (UITextInput it a lot of work/boilerplate) It makes sense for me to subclass UITextView. However, when subclassing it seems this is limited to TextKit 1 only, I get an assertion failure: *** Assertion failure in -[_UITextKit1LayoutController initWithTextView:textContainer:], _UITextKit1LayoutController.m:72 I thought I would just need to call the super init: super.init(usingTextLayoutManager: true) But this isn't a designated initialiser: Must call a designated initializer of the superclass 'UITextView' Is there a way to do this and override the layout manager so that it uses TextKit 2 in the subclass? (My aim is to then draw the fragments manually using TextKit2 to get a custom layout while ultimately using all of the UITextView implementation as 99% of it is what I want - other than custom drawing of text fragments). My code is below: class DocumentTextView: UITextView { private let _textLayoutManager = NSTextLayoutManager() private var textContentStorage: NSTextContentStorage { textLayoutManager!.textContentManager as! NSTextContentStorage } override var textLayoutManager: NSTextLayoutManager? { _textLayoutManager } init() { let textContainer = NSTextContainer(size: .zero) super.init(frame: .zero, textContainer: textContainer) _textLayoutManager.textContainer = textContainer textContentStorage.attributedString = NSAttributedString(string: text, attributes: [ .foregroundColor: UIColor.label, ]) textContentStorage.addTextLayoutManager(_textLayoutManager) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
2
0
563
Aug ’24
Deadlock in UIKit while injecting test bundle
Platform and Version iOS Development environment: Xcode 16.0 beta 5 (16A5221g), macOS 14.6.1 (23G93) Run-time configuration: iOS 18.0 beta 5 (22A5326g) Description of Problem Starting with iOS 18 SDK, test bundles containing a +load method that accesses UIScreen.mainScreen result in deadlock during test bundle injection. Also filed as FB14703057 I'm looking for clarity on whether this behavior is considered a bug in iOS or whether we will need to change the implementation of our app. Steps to Reproduce Create a new iOS app project using Objective-C and including unit tests Add the following code snippet to any .m file in the test target: @interface Foo: NSObject @end @implementation Foo + (void)load { UIScreen * const mainScreen = UIScreen.mainScreen; NSLog(@"%@", mainScreen); } @end Run the tests Expected Behavior As with iOS 17 & Xcode 15, the tests run to completion. Actual Behavior With iOS 18 & Xcode 16, deadlock during test bundle injection. stack_trace.txt
2
0
479
Aug ’24
Attempting to add scrubbing to UISlider
I made a custom slider by subclassing UISlider, and I'm trying to add scrubbing functionality to it, but for some reason the scrubbing is barely even noticeable at 0.1? In my code, I tried multiplying change in x distance by the scrubbing value, but it doesn't seem to work. Also, when I manually set the scrubbing speed to a lower value such as 0.01, it does go slower but it looks really laggy and weird?? What am I doing wrong? Any help or advice would be greatly appreciated! Subclass of UISlider: class SizeSliderView: UISlider { private var previousLocation: CGPoint? private var currentLocation: CGPoint? private var translation: CGFloat = 0 private var scrubbingSpeed: CGFloat = 1 private var defaultDiameter: Float init(startValue: Float = 0, defaultDiameter: Float = 500) { self.defaultDiameter = defaultDiameter super.init(frame: .zero) value = clamp(value: startValue, min: minimumValue, max: maximumValue) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func draw(_ rect: CGRect) { super.draw(rect) clear() createThumbImageView() addTarget(self, action: #selector(valueChanged(_:)), for: .valueChanged) } // Clear elements private func clear() { tintColor = .clear maximumTrackTintColor = .clear backgroundColor = .clear thumbTintColor = .clear } // Call when value is changed @objc private func valueChanged(_ sender: SizeSliderView) { CATransaction.begin() CATransaction.setDisableActions(true) CATransaction.commit() createThumbImageView() } // Create thumb image with thumb diameter dependent on thumb value private func createThumbImageView() { let thumbDiameter = CGFloat(defaultDiameter * value) let thumbImage = UIColor.red.circle(CGSize(width: thumbDiameter, height: thumbDiameter)) setThumbImage(thumbImage, for: .normal) setThumbImage(thumbImage, for: .highlighted) setThumbImage(thumbImage, for: .application) setThumbImage(thumbImage, for: .disabled) setThumbImage(thumbImage, for: .focused) setThumbImage(thumbImage, for: .reserved) setThumbImage(thumbImage, for: .selected) } // Return true so touches are tracked override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { let location = touch.location(in: self) // Ensure that start location is on thumb let thumbDiameter = CGFloat(defaultDiameter * value) if location.x < bounds.width / 2 - thumbDiameter / 2 || location.x > bounds.width / 2 + thumbDiameter / 2 || location.y < 0 || location.y > thumbDiameter { return false } previousLocation = location super.beginTracking(touch, with: event) return true } // Track based on moving slider override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { guard isTracking else { return false } guard let previousLocation = previousLocation else { return false } // Reference // location: location of touch relative to device // delta location: change in touch location WITH scrubbing // adjusted location: location of touch to slider bounds (WITH scrubbing) // translation: location of slider relative to device let location = touch.location(in: self) currentLocation = location scrubbingSpeed = getScrubbingSpeed(for: location.y - 50) let deltaLocation = (location.x - previousLocation.x) * scrubbingSpeed var adjustedLocation = deltaLocation + previousLocation.x - translation if adjustedLocation < 0 { translation += adjustedLocation adjustedLocation = deltaLocation + previousLocation.x - translation } else if adjustedLocation > bounds.width { translation += adjustedLocation - bounds.width adjustedLocation = deltaLocation + previousLocation.x - translation } self.previousLocation = CGPoint(x: deltaLocation + previousLocation.x, y: location.y) let newValue = Float(adjustedLocation / bounds.width) * (maximumValue - minimumValue) + minimumValue setValue(newValue, animated: false) sendActions(for: .valueChanged) return true } // Reset start and current location override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { self.currentLocation = nil self.translation = 0 super.touchesEnded(touches, with: event) } // Thumb location follows current location and resets in middle override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect { let thumbDiameter = CGFloat(defaultDiameter * value) let origin = CGPoint(x: (currentLocation?.x ?? bounds.width / 2) - thumbDiameter / 2, y: (currentLocation?.y ?? thumbDiameter / 2) - thumbDiameter / 2) return CGRect(origin: origin, size: CGSize(width: thumbDiameter, height: thumbDiameter)) } private func getScrubbingSpeed(for value: CGFloat) -> CGFloat { switch value { case 0: return 1 case 0...50: return 0.5 case 50...100: return 0.25 case 100...: return 0.1 default: return 1 } } private func clamp(value: Float, min: Float, max: Float) -> Float { if value < min { return min } else if value > max { return max } else { return value } } } UIView representative: struct SizeSlider: UIViewRepresentable { private var startValue: Float private var defaultDiameter: Float init(startValue: Float, defaultDiameter: Float) { self.startValue = startValue self.defaultDiameter = defaultDiameter } func makeUIView(context: Context) -> SizeSliderView { let view = SizeSliderView(startValue: startValue, defaultDiameter: defaultDiameter) view.minimumValue = 0.1 view.maximumValue = 1 return view } func updateUIView(_ uiView: SizeSliderView, context: Context) {} } Content view: struct ContentView: View { var body: some View { SizeSlider(startValue: 0.20, defaultDiameter: 100) .frame(width: 400) } }
7
0
434
Aug ’24