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.

All subtopics

Post

Replies

Boosts

Views

Activity

TextField("x", value: $y, format: .currency(code: "USD")) Behaves oddly
Hello, If we have the following code: import SwiftUI struct ContentView: View { @State private var usdAmount = 0.0 var body: some View { VStack { Text("Currency Form") Form { TextField("Dollar Amount", value: $usdAmount, format: .currency(code: "USD")) // 1 // TextField("Dollar Amount", value: $usdAmount, format: .number) // 2 .keyboardType(.numberPad) } Text("usdAmount = \(usdAmount)") } .padding() } } When the line marked // 1 is left uncommented (the TF has a .currency() formatter applied) the behaviour of the TF is rather odd. Upon display it shows US$0.00 Upon editing the value the US$ part remains. So if the user enters US$123 the value is not stored into the usdAmount property. The User must delete all the existing text and enter e.g. 456.78 and then the value is stored to the usdAmount property. When Line //1 is commented-out and line //2 is uncommented then the TF behaves correctly because the TF value is just 0 on first display. Should we not use the .currency() format? Or am I using it wrong? Thanks.
0
0
14
56m
Problem with new UITabbarController on IOS 18 beta (Bug?)
In my project, I create a UITabbarController using the storyboard. Later, I change the images and title of the individual tabbar items in the code. This works with the new tabbar controller from iOS 18, with one exception: If the iPad switches to a horizontally compact mode and thereby triggers the old tabbar (at the bottom of the screen), the images and titles from the storyboard appear there, not the values ​​changed in the code. Am I doing something wrong or is this a bug?
2
0
46
5h
.searchble Keyboard Incorrectly Showing After Presenting Popup
I have a list view that is accessed by pressing a button from a pervious view, all are which are in a NavigationView from the Main view. For this list view I have applied the .searchable tag to allow users to search through the list. When a user taps on an item in the list a .sheet will present to present a more detailed overview. The issue happens that if a user searches for an item in the list, and taps on the item to present the sheet the keyboard will disappear while the sheet presents but then pop back up despite still editing the search bar on the list view. Currently, my view is roughly set up like this. var body: some View { ThemesListView } .sheet(item: $presentedSheetTheme, onDismiss: { fetchedThemes.nsSortDescriptors = themeListSort.nsSortDescriptorObject }) {themeToPassOn in EditThemeView(themeToEdit: themeToPassOn) } var ThemesListView: some View { List { Button(action: { presentedSheetTheme = theme } } .searchable(text: query, placement: .navigationBarDrawer(displayMode: .always)) } I have tried many things to fix this, even finding this thread (https://forums.developer.apple.com/forums/thread/746826) however none of the solutions were any help. Here is a link (https://imgur.com/a/KKJ27iY) to a video of the issue. This issue seems to be fixed in some iOS versions such as 17.0 and 18.0 Beta 1 however many other versions such as 17.4 or 17.5 still experience this issue meaning I still need to fix it for these versions. Any help would be greatly appreciated, thanks!
0
0
41
9h
UITextView with large `textContainerInset` scrolls upwards when selecting text
Whenever a UITextView has a large textContainerInset it starts scrolling upwards rapidly when selecting text. For my use case, I have a custom view that I display in the text view to show the comment you are writing a reply to. To accomplish this, I use the textContainerInset and set the top portion of that inset to a large value. Here is a video showing the issue: https://streamable.com/9z57ok I've also made a very simple sample project to demonstrate and here is the main view controller code: https://gist.github.com/rickharrison/9cf563bcb22130bfafc7d3b5d37c55f2 Is it possible to disable scrolling of the UITextView while the selection UI is shown? Or is there another way I should inset the text to edit? I found another thread with this issue from years ago, but no solution - https://developer.apple.com/forums/thread/129882
0
0
39
10h
NavigationBar Back Button Color
I am trying to change the colour of the "Back" from blue to white but having difficulty do so, if anyone can suggest me a better way to do it would be grateful. "import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let appearance = UINavigationBarAppearance() appearance.configureWithOpaqueBackground() appearance.backgroundColor = UIColor(red: 0.216, green: 0.776, blue: 0.349, alpha: 1) appearance.titleTextAttributes = [.foregroundColor: UIColor.white] appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().scrollEdgeAppearance = appearance UINavigationBar.appearance().tintColor = .white return true } }"
0
0
50
13h
No containerBackground content on Widget in iOS 18 tinted home screen style
The widget I have create for iOS 17 uses the containerBackground to display an image in the background. This works fine. But when I set the home screen to the iOS 18 tinted option the background disappears. I want the background to stay because it contains an image of that is meaningful to the user. I use the following code: @ViewBuilder var body: some View { if let memory = entry.memory, let uiImage = memory.image { Group { if entry.showCaption { memoryBody(with: memory) } else { Color.white.opacity(0.0000000001) } } .foregroundStyle(.white) .widgetBackground( Image(uiImage: uiImage) .resizable() .scaledToFill() ) } else if let memory = entry.memory { memoryBody(with: memory) .widgetBackground(Color.gray) } else { noMemoryBody() } } extension View { func widgetBackground(_ backgroundView: some View) -> some View { if #available(iOSApplicationExtension 17.0, *) { return containerBackground(for: .widget) { backgroundView } } else { return background(backgroundView) } } }
1
0
62
18h
selection multiple buttons
struct Question { var title: String var options : [String] } struct SimpleQuestions: View { @State var selectedOption = "" @State var question = Question(title: "question title", options: \["one", "two", "three"\]) var body: some View { VStack { Text(question.title) ForEach(question.options, id: \.self) { option in HStack { Button { selectedOption = option } label: { Circle() .foregroundStyle(selectedOption == option ? .blue : .gray.opacity(0.5)) .frame(width: 23, height: 23) }}}}}
2
0
43
19h
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
51
22h
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
0
53
1d
Live Activity compact timers
The Human Interface Guidelines for Live Activities provide several examples of compact presentations that have a timer counting down formatted like "3min" or "3m", similar to the timers in the Clock app. Such examples can be found in this Stack Overflow question: https://stackoverflow.com/questions/77551496/is-it-possible-to-make-an-only-minutes-1m-countdown-in-a-live-activity A Timer initialized with init(timerInterval:pauseTime:countsDown:showsHours:) has provided a live countdown timer in widgets and live activities, but the formatting is limited. And since live activities come with limitations that prevent us from using Timers, there's no real way to implement this kind of thing ourselves. What is Apple's official guidance for implementing this kind of timer for the compact presentation?
1
0
73
1d
How to disable exporting a content from the Quicklook preview in vision OS 2.0?
It is pretty cool that you can preview Spatial Photos and Videos with just PreviewApplication.push(...), but the issue is, you can disable the editing, but buttons to export content to Files still going to be there. So if my application uses this to showcase some proprietary or licensed content in my application, I cannot really disable an option of exporting it from my app. And another, more major issue, a lot of developers would not even know that user can save Previewed item, as this Save to Files button is not that easy to find.
0
0
64
1d
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
46
1d
Delete button in default NSSavePanel for new document
I just noticed that when closing a new document with edits in MacOS Sonoma that it skips the Save/Don't Save/Cancel panel and goes directly to default NSSavePanel with Delete/Cancel/Save buttons. The problem is that when I click "Delete" nothing happens. It should have simple solution, but I could not find anything. How does one respond to the "Delete" button? My undocumented (as far as I can tell) hack was to implement document:didSave:contextinfo selector for runModalSavePanelForSaveOperation. It appears that in this method for a new document: Delete button has didSave=YES (even though it did not save) and the document fileURL nil Cancel button has didSave=NO and document fileURL nil Save button has didSave=YES and document filieURL to saved file I can handle Delete button this way, but since it is not a documented method, it make me uncomfortable. For example what happens is user clicks "Save", but the save has an error? As an aside, since Apple is now working with ChatGPT, I thought it might provide some help. I asked it how I can respond to "Delete" button in MacOS Sonoma and it said to implement deleteDocument: in your NSDocument subclass. I pointed out to ChatGPT that deleteDocument: does not exist. It said "you are correct" and you should instead check the returned result from runModalSavePanelForSaveOperation and look for "stop" action. I pointed out to ChatGPT that runModalSavePanelForSaveOperation is void and does not return a result, it said again, "you are correct." It gave a third option which basically said to override runModalSavePanelForSaveOperation and build your own save panel from scratch. I didn't know if I should trust this answer. I reverted to my hack and wrote this post. Also ChatGPT never apologized for wasting my time with the wrong answers.
3
0
62
1d
ShareLink Rich URL Previews in Messages
I am using ShareLink to share a URL. That URL is using the opengraph tags for audio (og:audio). If a users taps messages on the share sheet before the preview is rendered then sends the messages, they never see the rich URL preview. I don't know if this is a bug in the share sheet or if there is something I should be doing differently. If the user waits until the preview is rendered on the share sheet everything works fine. Also, if I go to Safari and share the same URL it works as expected.
3
0
72
1d