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
Posts under UI Frameworks topic

Post

Replies

Boosts

Views

Activity

VStack Alignment Issue
Hi Apple developer, I'm totally new in the development world using SwiftUI with just a little experience of Flutter. Currently I'm struggling with the combination of VStack and the alignment of the sub views. I'm using a LazyVGrid with multiple Rectangles and overlays inside. The overlay contains a VStack with a leading alignment. And here is the problem. I framed them with a border to visualize the limits of these views. But it seams, that the leading alignment is not working properly. There is a large gap on the left side of the Image() and Text() views and I don't know why. I'm very happy for any advice. Here is my code of this view. Thanks a lot! import SwiftUI import SwiftData extension UIScreen { static let screenWidth: CGFloat = UIScreen.main.bounds.size.width static let screenHeight: CGFloat = UIScreen.main.bounds.size.height static let screenSize: CGSize = UIScreen.main.bounds.size } struct TestView: View { let constants: Constants = Constants() let columnCount: Int = 2 let gridSpacing: CGFloat = 10 let gridRadius: CGFloat let gridWidth: CGFloat let gridHeight: CGFloat let gridItems = [ Item(title: "Total", color: Color.gray, image: "dollarsign.circle.fill") ] @State private var isSheetPresented: Bool = false init() { gridWidth = UIScreen._screenWidth / 2 - 3 * gridSpacing gridHeight = 1.25 * gridWidth gridRadius = gridWidth / 7.5 } var body: some View { NavigationStack { ScrollView(.vertical) { LazyVGrid(columns: Array( repeating: .init(.flexible(), spacing: -2 * gridSpacing), count: columnCount), spacing: 2 * gridSpacing) { ForEach(gridItems, id: \.title) { item in RoundedRectangle(cornerRadius: gridRadius, style: .continuous) .fill(item.color.gradient) .frame(width: gridWidth, height: gridHeight) .overlay( VStack(alignment: .leading) { Image(systemName: item.image) .colorInvert() .font(.system(size: 30)) .border(Color.yellow) Spacer() Text(item.title) .font(.system(size: 20)) .foregroundColor(.white) .border(Color.yellow) } .padding([.top, .bottom]) .frame(maxWidth: .infinity) .border(Color.black) ) } }.padding(.top) } .navigationTitle("Test View") .navigationBarTitleDisplayMode(.inline) .toolbar { ToolbarItem(placement: .topBarTrailing) { Menu { Button("Add", action: {}) } label: { Image(systemName: "ellipsis.circle") } } } } } } struct Item: Identifiable { let id: UUID = UUID() let title: String let color: Color let image: String } #Preview { TestView() }
Topic: UI Frameworks SubTopic: SwiftUI
1
0
369
Jan ’25
Poor ScrollView performance
Hello, I have a scroll view that when it appears I get a constant stream of warnings from the console: CGBitmapContextInfoCreate: CGColorSpace which uses extended range requires floating point or CIF10 bitmap context This happens whether or not I'm actively scrolling, so maybe it's not a scroll view issue at all? The reason I initially thought it was a scrollView issue was because when I scrolled the scrolling was no longer smooth, it was pretty choppy. Also, I only get these warnings when I run my code on a real device - I do not get these on any simulators. Could it be the mesh gradient causing an issue? I'm not sure what's causing the issue so I apologize in advance for what may be irrelevant code. struct ModernStoryPicker: View { @Environment(CategoryPickerViewModel.self) var categoryPickerViewModel @EnvironmentObject var navigationPath: GrowNavigationState @State private var hasPreselectedStory: Bool = false @State private var storyGenerationType: StoryGenerationType = .arabicCompanion var userInstructions: String { if categoryPickerViewModel.userInputCategory.isEmpty { "Select a category" } else if categoryPickerViewModel.userInputSubCategory.isEmpty { "Select a subcategory" } else { "Select a story" } } var body: some View { ZStack { MeshGradient(width: 3, height: 3, points: [ [0.0, 0.0], [0.5, 0.0], [1.0, 0.0], [0.0, 0.5], [0.9, 0.3], [1.0, 0.5], [0.0, 1.0], [0.5, 1.0], [1.0, 1.0] ], colors: [ .orange, .indigo, .orange, .blue, .blue, .cyan, .green, .indigo, .pink ]) .ignoresSafeArea() VStack { Picker("", selection: $storyGenerationType) { ForEach(StoryGenerationType.allCases) { type in Text(type.rawValue).tag(type) } }.pickerStyle(.segmented) Text(userInstructions) .textScale(.secondary) automatedOrUserStories() Spacer() }.padding() } .onAppear {...} } @ViewBuilder func automatedOrUserStories() -> some View { switch storyGenerationType { case .userGenerated: VStack { Spacer() Text("Coming soon!") } case .arabicCompanion: VStack { // TODO: Unnecessary passing of data, only the 2nd view really needs all these props CategoryPickerView( categories: categoryPickerViewModel.mainCategories(), isSubCategory: false, selectionColor: .blue ) CategoryPickerView( categories: categoryPickerViewModel.subCategories(), isSubCategory: true, selectionColor: .purple ) ScrollView(.horizontal) { HStack { if categoryPickerViewModel.booksForCategories.isEmpty { Text("More books coming soon!") } ForEach(categoryPickerViewModel.booksForCategories) { bookCover in Button(action: { // navigates to BookDetailView.swift navigationPath.path.append(bookCover) }) { ModernStoryCardView( loadedImage: categoryPickerViewModel.imageForBook(bookCover), bookCover: bookCover ) .scrollTransition(axis: .horizontal) { content, phase in content .scaleEffect(phase.isIdentity ? 1 : 0.5) .opacity(phase.isIdentity ? 1 : 0.8) } }.buttonStyle(.plain) } }.scrollTargetLayout() } .contentMargins(80, for: .scrollContent) .scrollTargetBehavior(.viewAligned) }.padding() } } } struct CategoryPickerView: View { @Environment(CategoryPickerViewModel.self) var viewModel let categories: [String] let isSubCategory: Bool let selectionColor: Color var body: some View { ScrollView(.horizontal) { HStack { ForEach(categories, id: \.self) { category in Button(action: { withAnimation { selectCategory(category) } }) { TextRoundedRectangleView(text: category, color: effectiveColor(for: category)) .tag(category) }.buttonStyle(.plain) } } }.scrollIndicators(.hidden) } private func selectCategory(_ string: String) { if !isSubCategory { viewModel.userInputCategory = string } else { viewModel.userInputSubCategory = string } } private func effectiveColor(for category: String) -> Color { let chosenCategory = isSubCategory ? viewModel.userInputSubCategory : viewModel.userInputCategory if category == chosenCategory { return selectionColor } return .white } }
4
0
650
Jan ’25
Type 'AVPlayer.Type' cannot conform to 'ObservableObject'
I'm having the following issue: Type 'AVPlayer.Type' cannot conform to 'ObservableObject' struct MusicEditorView: View { @ObservedObject var audioPlayer = AVPlayer and this is the class: class MusicPlayer: ObservableObject { private var audioPlayer: AVPlayer? private var timer: Timer? func playSound(named sFileName: String){ if let url = Bundle.main.url(forResource: sFileName, withExtension: "mp3"){ audioPlayer = try? AVPlayer(url: url) audioPlayer?.play() } } func pause(){ audioPlayer?.pause() } func getcurrentProgress() -> Double{ guard let currentTime = audioPlayer?.currentItem?.currentTime().seconds else { return 0 } guard let duration = audioPlayer?.currentItem?.duration.seconds else { return 0 } return duration > 0 ? (currentTime / duration) * 100 : 0 } func startProgressTimer(updateProgress: @escaping (Double, Double) -> Void){ timer?.invalidate() timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in guard let currentTime = self.audioPlayer?.currentItem?.currentTime().seconds else { return } guard let duration = self.audioPlayer?.currentItem?.duration.seconds else { return } updateProgress(currentTime, duration) } } func stopProgressTimer(){ timer?.invalidate() } struct Sound: Identifiable, Codable { var id = UUID() var name: String var fileName: String } } }
1
0
431
Jan ’25
macOS dark mode while window is in background
Hi, I detect dark mode on macOS like following: NSAppearance *appearance = NSApp.mainWindow.effectiveAppearance; NSString *interface_style = appearance.name; NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]]; if([basicAppearance isEqualToString:NSAppearanceNameDarkAqua]){ theme = "Adwaita:dark"; dark_mode = TRUE; } if([interface_style isEqualToString:NSAppearanceNameDarkAqua]){ theme = "Adwaita:dark"; dark_mode = TRUE; }else if([interface_style isEqualToString:NSAppearanceNameVibrantDark]){ theme = "Adwaita:dark"; dark_mode = TRUE; }else if([interface_style isEqualToString:NSAppearanceNameAccessibilityHighContrastAqua]){ theme = "HighContrast"; }else if([interface_style isEqualToString:NSAppearanceNameAccessibilityHighContrastDarkAqua]){ theme = "HighContrast:dark"; dark_mode = TRUE; }else if([interface_style isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantDark]){ theme = "HighContrast:dark"; dark_mode = TRUE; } But this doesn't work if my window is in background. As the application window is put into background, it loses dark mode. Howto fix it? regards, Joël
2
0
561
Jan ’25
Disabling App Clip's Initial App Store Notification Banner
We're encountering a UX challenge with the automatic App Store notification banner that appears when users first launch our App Clips (not the App Clip sheet). This notification, which suggests downloading the full app, is creating confusion among our users. We've observed that some users tap the notification instead of completing their intended action within the App Clip, interrupting their workflow. Is there a way to disable this banner?
0
1
350
Jan ’25
Is it possible to disable tab switching or prevent interaction with CPTabBarTemplate in CarPlay?
Hello, I'm currently developing a CarPlay app using the CPTabBarTemplate, and I need to know if it's possible to disable user interaction with the tab bar, specifically preventing the user from switching between tabs or tapping on the tab items. Is there a way to lock the selected tab or ignore tab switching attempts programmatically? I would like to maintain the tab bar UI but prevent users from changing tabs or interacting with it while in a certain mode or condition.
1
0
441
Jan ’25
Rectangle change size by swipe
Hi,, How can I make a rectangle become taller as I swipe down on my trackpad?" struct BlueRectangleView: View { var body: some View { VStack { Rectangle() .fill(Color.blue) .frame(width: 200, height: 100) .cornerRadius(10) .shadow(radius: 5) .padding() } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) } } struct BlueRectangleView_Previews: PreviewProvider { static var previews: some View { BlueRectangleView() } }
2
0
277
Jan ’25
SwiftUI - Drag gesture blocks scroll gesture only on iPhone 11
I'm pretty new to Swift and SwiftUI. I'm making my first app for sorting a gallery with some extra features. I was using my own iPhone for testing and just started testing my app on other Apple products. Everything works fine on iPad Air M1, iPhone 15 Pro, iPhone 15 Pro Max, iPhone 13, iPhone XS (Simulator), and iPhone 11 Pro (Simulator). However, when I tried to show my app to a family member with an iPhone 11, I came across an issue. Issue Description: My app takes all photos from iPhone's native gallery, then you can sort it by some spesific filters and delete pictures. It just looks like the native gallery. (I can add photos later if needed) You can just scroll the gallery by swiping up and down. You can press the select button and start selecting pictures to delete. I recently added a drag-to-select-multiple-pictures feature. This makes it feel more like the native iOS experience, eliminating the need to tap each picture individually. However, on the iPhone 11, the moment you open the app, you can't scroll. Scrolling is completely locked. You can still select pictures by tapping or dragging, so it's not a touch area issue. The same issue persists on the iPhone 11 simulator. And I think I found the problematic part in my (sadly messy) ContentView.swift file; ScrollView { RefreshControl(coordinateSpace: .named("refresh")) { await viewModel.refreshMediaItems() } LazyVGrid(columns: gridColumns, spacing: UIDevice.current.userInterfaceIdiom == .pad ? 12 : 4) { let items = viewModel.filteredItems(typeFilter: mediaTypeFilter, specialFilter: specialFilter) ForEach(Array(zip(items.indices, items)), id: \.1.id) { index, item in MediaThumbnailView( item: item, isSelected: selectedItems.contains(item.id), viewModel: viewModel, onLongPress: { if !isSelectionMode { toggleSelectionMode() selectedItems.insert(item.id) } }, onTap: { if isSelectionMode { toggleSelection(item: item) } else { viewModel.selectItem(item) } } ) .aspectRatio(1, contentMode: .fit) .background( GeometryReader { geometry in let frame = geometry.frame(in: .named("grid")) Color.clear.preference( key: ItemBoundsPreferenceKey.self, value: [ItemBounds(id: item.id, bounds: frame, index: index)] ) } ) } } .padding(.horizontal, 2) .coordinateSpace(name: "grid") .onPreferenceChange(ItemBoundsPreferenceKey.self) { bounds in itemBounds = Dictionary(uniqueKeysWithValues: bounds.map { ($0.id, $0) }) itemIndices = Dictionary(uniqueKeysWithValues: bounds.map { ($0.id, $0.index) }) } .gesture( DragGesture(minimumDistance: 0) .onChanged { gesture in if isSelectionMode { let location = gesture.location if !isDragging { startDragging(at: location, in: itemBounds) } updateSelection(at: location, in: itemBounds) } } .onEnded { _ in endDragging() } ) } .coordinateSpace(name: "refresh") } you can see the .gesture(.... part. I realised that this DragGesture and ScrollView blocks each other (somehow only on iPhone 11) highPriorityGesture also won't work. When I change it with simultaneousGesture, scroll starts to work again. BUT - since it's simultaneous, when multiple selection mode is activated, when I'm dragging my finger gallery also starts to scroll and it becomes a very unpleasant experience. After this issue I realised on native gallery iOS locks scroll when you are dragging for multiple selection and just when you release your finger you can scroll again even if the multiple selection mode is active. I tried a million things, asked claude, chatgpt etc. etc. Found some similar issues on stackoverflow but they were all related to iOS 18, not spesific to an iPhone. My app works fine on iOS 18 (15 Pro Max) iOS 18 drag gesture blocks scrollview Here are the some of the things I've tried: using highPriorityGesture and simultenousgesture together, tried to lock the scroll briefly while dragging, implement much complicated versions of these things with the help of claude, try to check if isSelectionMode is true or not All of them broke other things/won't work. Probably there's something pretty simple that I'm just missing; but iPhone 11 being the single problematic device confuses me. I don't want to mess too much with my already fragile logic.
1
0
776
Jan ’25
Adding a Label with UIImage and Text to the TabSection Header in tvOS 18+
I've been trying to add a header to the tabSection of the tabview in tvos 18+ . init( @TabContentBuilder<SelectionValue> content: () -> Content, @ViewBuilder header: () -> Header ) where Header : View, Footer == EmptyView Here the ehader clearly conforms to View but i cant quite fit the label with uiimage as the icon into this. This Label when i add it to any other view, the image is in the specified 50 x 50 size but inside header it functions weirdly to be of a huge size. but also to note, if i simply hav an icon here, it is correct. So what is the problem here.. can someone help me? im supposed to add the user profile and name in the header. I dont think there's any other way
Topic: UI Frameworks SubTopic: SwiftUI Tags:
0
0
352
Jan ’25
SwiftUI flash animation
I'm struggling to implement a flash animation in SwiftUI. Generally animations animate from one value to another. I'd like to animate from normal to the flashed state and then back to normal, each time the data shown by the view changes. The "flashed state" could be transparent, or a white background, or it could be a scale change for a pulse effect, or something. Example: struct MyView: View { let value: String; var body: some View { ZStack { Capsule() .fill(Color.green); Text(value); } }; }; Each time value changes, I'd like the colour of the capsule to quickly animate from green to white and back to green. I feel this should be easy - am I missing something? For bonus points: I'd like the Text to change to its new value at the midpoint of the animation, i.e. when the white text is invisible on the white background. I'd like to get the flash effect whenever I have a new value even if the new value is equal to the old value, if you see what I mean.
3
0
605
Jan ’25
Controlling the focus order in a UICollectionView
I'm running into a problem with the focus order in my UICollectionView in my tvOS app. The layout of my app is outlined in the following diagram On the uppermost layer I have a UITableView, each cell containing a single UICollectionView that can contain a variable number of UICollectionViewCells. When the user swipes down, I want the left-most item in the next row down to be selected, as follows: I've been able to get this to work for the majority of cases. The exception is when the previously focused item extends to the length of the screen - in this case the next focused item isn't the leftmost item, but rather the center item, as follows. Using focus guides won't really fit the use case here since collection views are dynamically generated. I've found that there is a function in UICollectionViewDelegate, indexPathForPreferredFocusedView that should help here but it only seems to be called intermittedly, even though I have set remembersLastFocusedIndexPath on the collectionview to true. I can force it to be called by calling setNeedsFocusUpdate() but I can't figure out a good place to call it - shouldUpdateFocus is too early and didUpdateFocus is too late. Is there some way to constrain the focus area of a view to a certain subset of it's frame - for example, the left-most quarter of its width? Or does anyone know of some other solution to this problem?
1
0
563
Jan ’25
SwiftData Query and optional relationships
Xcode 16.2 (16C5032a) FB16300857 Consider the following SwiftData model objects (only the relevant portions are shown) (note that all relationships are optional because eventually this app will use CloudKit): @Model final public class Team { public var animal: Animal? public var handlers: [Handler]? ... } @Model final public class Animal { public var callName: String public var familyName: String @Relationship(inverse: \Team.animal) public var teams: [Team]? ... } @Model final public class Handler { public var givenName: String @Relationship(inverse: \Team.handlers) public var teams: [Team]? } Now I want to display Team records in a list view, sorted by animal.familyName, animal.callName, and handlers.first.givenName. The following code crashes: struct TeamListView: View { @Query<Team>(sort: [SortDescriptor(\Team.animal?.familyName), SortDescriptor(\Team.animal?.callName), SortDescriptor(\Team.handlers?.first?.givenName)]) var teams : [Team] var body: some View { List { ForEach(teams) { team in ... } } } } However, if I remove the sort clause from the @Query and do the sort explicitly, the code appears to work (at least in preliminary testing): struct TeamListView: View { @Query<Team> var teams: [Team] var body: some View { let sortedTeams = sortResults() List { ForEach(sortedTeams) { team in ... } } } private func sortResults() -> [Team] { let results: [Team] = teams.sorted { team1, team2 in let fam1 = team1.animal?.familyName ?? "" let fam2 = team2.animal?.familyName ?? "" let comp1 = fam1.localizedCaseInsensitiveCompare(fam2) if comp1 == .orderedAscending { return true } if comp1 == .orderedDescending { return false } ... <proceed to callName and (if necessary) handler givenName comparisons> ... } } } While I obviously have a workaround, this is (in my mind) a serious weakness in the implementation of the Query macro.
5
0
812
Jan ’25
learning coregraphics help: connecting line to circles
Hi everyone, im in the process of delving more into coregraphics with swiftui, but I am at a roadblock. First I would like to ask, what are some good resources to learn coregraphics? Secondly: I currently have a circle view made and what I want to do is to make my circle view modular so that it can be directly connected to another given circle by a line. How can I do this? For example, I want my circles to represent nodes and be able to connect by lines to other nodes that are related. Thanks in advanced. Here is my code for the circle view: @State private var circleProgress: CGFloat = 0 let timer = Timer.publish(every: 0.016, on: .main, in: .common).autoconnect() private let animationDuration: TimeInterval = 1.5 @Binding var startPoint: CGPoint @Binding var endPoint: CGPoint var body: some View { GeometryReader { geometry in Canvas { context, size in // Circle parameters let circleSize: CGFloat = 50 let circleOrigin = CGPoint( x: size.width / 4, y: size.height / 2 - circleSize / 2 ) let circleRect = CGRect( origin: circleOrigin, size: CGSize(width: circleSize, height: circleSize) ) let circleCenter = CGPoint( x: circleOrigin.x + circleSize / 2, y: circleOrigin.y + circleSize / 2 ) // Animate circle creation var circlePath = Path() circlePath.addArc( center: circleCenter, radius: circleSize / 2, startAngle: .degrees(0), endAngle: .degrees(360 * circleProgress), clockwise: false ) context.addFilter(.shadow(color: .white.opacity(0.6), radius: 5, x: 1, y: 1)) // Add white shadow context.stroke( circlePath, with: .linearGradient( Gradient(colors: [.purple, .white]), startPoint: circleRect.origin, endPoint: CGPoint(x: circleRect.maxX, y: circleRect.maxY) ), lineWidth: 5 ) } .frame(width: 300, height: 150) .onReceive(timer) { _ in // Update circle progress let progressChange = 0.02 / animationDuration if circleProgress < 1.0 { circleProgress = min(circleProgress + progressChange, 1.0) } else { circleProgress = 0.0 // Reset the circle to repeat the animation } // Get the starting and ending points of the Canvas view startPoint = CGPoint(x: geometry.frame(in: .global).minX, y: geometry.frame(in: .global).minY) endPoint = CGPoint(x: geometry.frame(in: .global).maxX, y: geometry.frame(in: .global).maxY) // Print the points for debugging print("Start Point: \(startPoint.x), \(startPoint.y)") print("End Point: \(endPoint.x), \(endPoint.y)") } } .frame(width: 300, height: 150) } }
1
0
543
Jan ’25
Carplay在CPNowPlayingTemplate显示图片的败,且不可点击
System: iOS 18.1.1 When connected to Carplay, after playing a song, check the playback page CPNowPlayingTemplate. This error appears on the BMW car, as shown in the picture: In our project, this is achieved using the following methods: UIImage *image1 = [UIImage imageNamed:@"imageName"];; CPNowPlayingImageButton *button1 = [[CPNowPlayingImageButton alloc] initWithImage:image1 handler:^(__kindof CPNowPlayingButton * _Nonnull action) { //do something }]; UIImage *image2 = [UIImage imageNamed:@"imageName"];; CPNowPlayingImageButton *button2 = [[CPNowPlayingImageButton alloc] initWithImage:image2 handler:^(__kindof CPNowPlayingButton * _Nonnull action) { //do something }]; NSArray<CPNowPlayingButton *> *buttons; buttons = @[button1,button2]; [[CPNowPlayingTemplate sharedTemplate] updateNowPlayingButtons:buttons]; Is there any way to solve this problem?
Topic: UI Frameworks SubTopic: UIKit Tags:
1
0
382
Jan ’25
SwiftUI Animation - Spooky Action At A Distance
I have a grid-like container with subviews. I recently changed some internal details of the subviews, so that changes to the values they display animate. Now, the behaviour of the grid container has changed: the animation duration used for the internal changes is now also used when the grid is re-ordered or subviews are added or removed. I can see why this happens: the grid repositions the subviews, and the subview has declared an animation that applies to all of its properties however they are modified. This doesn't seem like a good idea to me. The principle of encapsulation suggests that I should be able to make internal changes to a component without suffering "spooky action at a distance", i.e. other components unexpectedly changing their behaviour. Is this an inherent issue with SwiftUI animations, or does it suggest that I am doing something wrong?
1
0
319
Jan ’25
Validate drag operations with Transferable
Let's say I want to build a simple photo management app on Mac or iPad with Swift UI. This app has multi-window support. My photos are organized inside albums. I should be able to drag photos between windows from one album to another. I struggle to get this working properly with Swift UI. Writing modern code I would like to use Transferable. Let's say my photos are not real files. So I can't use a FileRepresentation. Instead I use CodableRepresentation and encode an identifier. This identifier is later used to drive the move operation between folders. I ran into some limitations here Transferable seems to be meant for copy-like Drag & Drop operations. I have no possible to get a "move" cursor on macOS (it's always the copy cursor with the green + sign). Also the API reads like it is about importing/exporting – not moving. When using dropDestination on ForEach, I completely lack the possibility to deny a drop (e.g. when a photo is already part of the album). I'd like to have modifier key to switch between copying and moving. Sometimes a drop should be redirected to a different index. How to do that? Is there any chance to do this with Transferable? It even doesn't seem to be easy with NSItemProvider and onDrop/onDrag? Or should we still use a plain old UICollectionView/NSCollectionView, if we want to have more sophisticated control over drag/drop validation?
4
0
442
Jan ’25