Post

Replies

Boosts

Views

Activity

Make a grid of `TextField`s in SwiftUI
Code that reproduces the issue import SwiftUI @main struct TextFieldsGridApp: App { @State private var controller = Controller() var body: some Scene { WindowGroup { GridView() .environment(controller) } } } struct GridView: View { @Environment(Controller.self) private var c var body: some View { VStack { ForEach(0..<c.strings.count, id: \.self) { r in HStack { ForEach(0..<4, id: \.self) { c in TextField( "", text: c.strings[r][c] ) .textFieldStyle(.roundedBorder) } } } } .padding() } } #Preview { GridView() .environment(Controller()) } @Observable class Controller { private(set) var strings: [[String]] = Array( repeating: Array(repeating: "A", count: 4), count: 4, ) } Error Cannot convert value of type 'Range<Int>' to expected argument type 'Binding<C>', caused by ForEach(0..<4, id: \.self) { c in. Which I do not get if I say, for example: struct GridView: View { @State private var text = "H" // ... TextField("", text: $text) Environment MacBook Air M1 8GB iOS Sequoia 15.5 Xcode 16.4 Preview: iPhone 16 Pro, iOS 18.5.
Topic: UI Frameworks SubTopic: SwiftUI
1
0
132
Jul ’25
iOS app simulated on mac doesn't get installed
When I simulate an app on an iOS device, the app gets installed, and is available for later use. When I do so on the mac and interrupt the simulation, I can still find the app afterwards, but, if I click on it, I get an alert that says that the app is not supported. My app's supported destinations are iPhone, iPad and Mac (designed for iPad). How do I install an Xcode app on a mac with an apple silicon chip?
0
0
572
Jun ’23
WWDC18 "Testing Tips & Tricks" approach to testing notification center makes unit tests share state
In the following code, test 1 (test_postNotification) fails while test 2 (test_notificationsArePostedOnTheMainQueue) passes. What concerns me, though, is that if I substitute the lines "let result = XCTWaiter.wait(for: [expectation], timeout: 0); XCTAssertEqual(result, .timedOut)" of test 2 with "wait(for: [expectation], timeout: 0.1)", then test number 1 passes. I have cleaned the build folder and restarted Xcode and my computer, but the issue persists. This concerns me because I would have said that the tests of the NotificationPosterTests class were isolated, but apparently they are not, since changing test 2 makes test 1 go from failing to passing. Is this expected behavior? import Foundation import XCTest extension Notification.Name { static let menuPostRequest = Notification.Name("menuPostRequest") static let editingOrderError = Notification.Name("editingOrderError") } class NotificationPoster { let notificationCenter: NotificationCenter init(notificationCenter: NotificationCenter = .default) { self.notificationCenter = notificationCenter } func postNotification(_ notification: Notification) { let _notificationCenter = notificationCenter // you can't use optional chaining nor conditional unwrapping on self to reference self.notificationCenter in the dispatch block because self is nil when self.postNotification(_:) is called DispatchQueue.main.async { _notificationCenter.post(notification) } } } final class NotificationPosterTests: XCTestCase { private var sut: NotificationPoster! private var notificationCenter: NotificationCenter! override func setUp() { super.setUp() notificationCenter = NotificationCenter() sut = NotificationPoster(notificationCenter: notificationCenter) } override func tearDown() { notificationCenter = nil sut = nil super.tearDown() } func test_postNotification() { let notification = Notification(name: .menuPostRequest) let expectation = XCTNSNotificationExpectation( name: notification.name, object: notification.object, notificationCenter: notificationCenter ) sut.postNotification(notification) wait(for: [expectation], timeout: 0.1) // don't make it 0.01 } func test_notificationsArePostedOnTheMainQueue() { let notification = Notification(name: .editingOrderError) let expectation = XCTNSNotificationExpectation( name: notification.name, object: notification.object, notificationCenter: notificationCenter ) sut.postNotification(notification) let result = XCTWaiter.wait(for: [expectation], timeout: 0) XCTAssertEqual(result, .timedOut) } }
0
0
544
Nov ’23
How do I center a UIImageView vertically in a UICollectionViewListCell as a custom accessory?
Please run the following UIKit app. It uses a collection view with compositional layout (list layout) and a diffable data source. It has one section with one row. The cell has an image view as a leading accessory. Unfortunately, as soon as I set an image for the image view, the accessory is no longer centered: import UIKit class ViewController: UIViewController { var collectionView: UICollectionView! var dataSource: UICollectionViewDiffableDataSource<String, String>! override func viewDidLoad() { super.viewDidLoad() configureHierarchy() configureDataSource() } func configureHierarchy() { collectionView = .init(frame: .zero, collectionViewLayout: createLayout()) view.addSubview(collectionView) collectionView.frame = view.bounds } func createLayout() -> UICollectionViewLayout { UICollectionViewCompositionalLayout { section, layoutEnvironment in let config = UICollectionLayoutListConfiguration(appearance: .insetGrouped) return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment) } } func configureDataSource() { let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, String> { cell, indexPath, itemIdentifier in let iv = UIImageView() iv.backgroundColor = .systemRed // iv.image = .init(systemName: "camera") iv.contentMode = .scaleAspectFit iv.frame.size = .init( width: 40, height: 40 ) cell.accessories = [.customView(configuration: .init( customView: iv, placement: .leading(), reservedLayoutWidth: .actual, maintainsFixedSize: true ))] } dataSource = .init(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier) } var snapshot = NSDiffableDataSourceSnapshot<String, String>() snapshot.appendSections(["main"]) snapshot.appendItems(["demo"]) dataSource.apply(snapshot, animatingDifferences: false) } } This seems like a bug but then if I set the image view's size to 100x100, even without giving it an image, the cell doesn't resize, which makes me think I'm making a mistake.
Topic: UI Frameworks SubTopic: UIKit Tags:
0
0
511
Apr ’24
How to scale a SwiftUI view to fit its parent
Code that reproduces the issue import SwiftUI @main struct KeyboardLayoutProblemApp: App { var body: some Scene { WindowGroup { iOSTabView() } } } struct iOSTabView: View { var body: some View { TabView { GameView() .frame(maxWidth: UIScreen.main.bounds.width, maxHeight: UIScreen.main.bounds.height) .tabItem { Label("Play", systemImage: "gamecontroller.fill") } } } } struct GameView: View { var body: some View { VStack { Text("Play") Spacer() KeyboardView() } .padding() } } struct KeyboardView: View { let firstRowLetters = "qwertyuiop".map { $0 } let secondRowLetters = "asdfghjkl".map { $0 } let thirdRowLetters = "zxcvbnm".map { $0 } var body: some View { VStack { HStack { ForEach(firstRowLetters, id: \.self) { LetterKeyView(character: $0) } } HStack { ForEach(secondRowLetters, id: \.self) { LetterKeyView(character: $0) } } HStack { ForEach(thirdRowLetters, id: \.self) { LetterKeyView(character: $0) } } } .padding() } } struct LetterKeyView: View { let character: Character var width: CGFloat { height*0.8 } @ScaledMetric(relativeTo: .title3) private var height = 35 var body: some View { Button { print("\(character) pressed") } label: { Text(String(character).capitalized) .font(.title3) .frame(width: self.width, height: self.height) .background { RoundedRectangle(cornerRadius: min(width, height)/4, style: .continuous) .stroke(.gray) } } .buttonStyle(PlainButtonStyle()) } } Problem GameView doesn't fit its parent view: Question How do I make GameView be at most as big as its parent view? What I've tried and didn't work GameView() .frame(maxWidth: .infinity, maxHeight: .infinity) GeometryReader { geometry in GameView() .frame(maxWidth: geometry.size.width, maxHeight: geometry.size.height) } GameView() .clipped() GameView() .layoutPriority(1) GameView() .scaledToFit() GameView() .minimumScaleFactor(0.01) GameView() .scaledToFill() .minimumScaleFactor(0.5) I'm not using UIScreen.main.bounds.width because I'm trying to build a multi-platform app.
Topic: UI Frameworks SubTopic: SwiftUI
0
0
89
Jul ’25