I have a ZStack with two nested views. They are both the same type, RowView, and each RowView has another ZStack and a Text.
Both of these RowViews have the same height for some reason. And when I replace the RowView ZStack with just a Text, it works correctly.
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView {
		 // remove this ZStack and things work with the other nested ZStacks
ZStack {
Rectangle()
.foregroundColor(.blue)
.shadow(radius: 5, x: 5, y: 5)
VStack {
// Two independent RowViews:
RowView(title: "Some really long text for some stuff lollola really long text for some stuff lollola")
RowView(title: "and more")
}
}
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct RowView: View {
@State var title: String
let boxSize: CGFloat = 40
var body: some View {
HStack(alignment: .top, spacing: nil) {
Rectangle()
.foregroundColor(.orange)
.frame(minWidth: boxSize,
maxWidth: boxSize,
minHeight: boxSize,
maxHeight: boxSize)
.padding(5)
// this has each RowView set its own height correctly
// Text(title)
// .multilineTextAlignment(.leading)
// .padding(5)
// this forces both RowViews to have the same height, even though they are separate from each other
ZStack {
Rectangle()
.foregroundColor(.gray)
Text(title)
.multilineTextAlignment(.leading)
.padding(5)
}
.padding(5)
// this also forces both RowViews to have the same height
// Rectangle()
// .foregroundColor(.gray)
// .overlay(Text(title).padding())
// .padding(5)
}
}
}
It seems strange that both independent RowViews will end up having the same height.
Read the release notes and didn't see mention of this being a possible bug.
Copy pasta and see the results if you'd like. Notice the Gray ZStack containers are all almost the same height? Make the test of the first row even longer. Notice how the second one increases it's height?
Is this a bug,? Are ZStacks never supposed to be nested?
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.
Post
Replies
Boosts
Views
Activity
I have recently created a pure (admittedly simple) SwiftUI app for iOS/iPad. Yes there are bugs and limitations I've had to face but eventually with a certain amount of compromise and without resorting to UIViewRepresentable, it works adequately.
Great, I thought, how hard can it be to create a macOS version of the same app? Take TextField (swiftUI) views which my app depends on a lot, the issues I have found have been numerous...
TextField does not appear to update the Binded variable after each character is typed in. You have to hit the return key for it to register. Totally different functionality.
Placeholder text shifts up a few pixels when it gets keyboard focus.
The rectangle where the text is typed needs to be taller holding the text, currently it looks squashed.
Manually adding a ‘clear text’ button on top of the TextField (at the right) appears not to to be active when the cursor is over it (most of the time)
Lots of missing autocapitalisation type functionality is missing.
I could go back to a NSViewRepesentable solution of the TextField but that negates the use of 'supported' SwiftUI features. Has this half baked feature been pushed out there as a 'tick in the box' option or is Apple genuinely happy with their solution?
Anyhow, I thought let's do a MacCatalyst version of my App instead. But we get a TabView looking like a iPad/iPhone App, there is no option to make it look more mac like AFAIS without abandoning TabView itself!
Then there's the complication of making my Core Data App work as a 'Document Based' app with the new DocumentGroup/Scene solution.... how does NSPersistentDocument work in such scenarios? The documentation is vague at best, or simply not supported without a lot of workarounds.
Just these few things make me feel we are being hyped with solutions which are far too premature for any real world work on macOS at the moment.
What potential SwiftUI/macOS blockers have you encountered?
I have a collection view with UICollectionViewDiffableDataSource, which has class PHAssetVisualAssetsPickerItem as it’s ItemIdentifierType. When I create instance of NSDiffableDataSourceSnapshot with the same items that dataSource already has, and then apply it, I see that collection view blinks even tho nothing changed. It actually blinks no matter what new snapshot is.
My investigation led to a fact that methods == (Equatable) and hasher(into:) (Hashable) are not called on items if items are classes. If I change them to structs, methods are called, dataSource understands what to do, nothing blinks.
https://www.icloud.com/iclouddrive/0vnB3auwp0ShEvPQmpVOAy6fg#ApplySnapshotIndexPaths
Here is the code where you can simply change HashableType to either class or struct and see that collectionView update looks different when you tap button.
In WatchOS 6 a presented .sheet could be dismissed by swiping down or tapping on the navigationBarTitle. In the WatchOS 7 beta, presented sheets are no longer dismissible by either method...forcing the addition of a button to dismiss.
It appears as if .sheet is now acting as .fullScreenCover within WatchOS 7.
Anyone else running into this? Wondering if this is now expected behavior on WatchOS or if it's a bug...
I am presenting a sheet using the view modifier
.sheet(item: ...)
if the view you are presenting is wrap inside another view which only add a navigation view
let's say I have a view called ViewB with a dismiss button which called the presentation mode dismiss, that works
let' say I have another view called ViewBWithNavigationView
where it's calling ViewB inside the NavigationView and it also calling dismiss on the cancel button
the result of this is that the presentation mode dismiss will only works for the first view B the cancel button, it will no close the view if you click the button inside the view
struct ViewBInsideNavigationView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView {
ViewB()
.navigationBarTitle("View B", displayMode: .inline)
.navigationBarItems(leading: Button("Cancel") {
self.presentationMode.wrappedValue.dismiss()
})
}
}
}
the call to self.presentationMode.wrappedValue.dismiss() inside ViewB is not trigger and the view is not dismiss
thanks
The two property wrappers @AppStorage and @SceneStorage are great because you can simply redeclare a property in multiple views to share the value and automatically update other views.
But how should I access those same values outside of views?
I have helper functions which are used in multiple views and which change behaviour based on those stored properties. I believe I could use the old NSUserDefaults.standard to reference the @AppStorage values, but is that the "right" way to do it, and how do I access @SceneStorage values?
Sometime, when I open the Widget Gallery, function getSnapshot(for:in:completion:) in TimelineProvider is not called. Is this a feature or a bug? When is this function called?
Thank you.
Can anyone confirm that there is at present no equivalent on the TextField view to the UITextFieldDelegate method
textField(_:shouldChangeCharactersIn:replacementString:)
i.e. one that can intercept each keystroke and and paste to ensure that the value is always valid?
In SwiftUI the onEditingChanged parameter in the constructor is not triggered with each character entered.
In the following environment: iOS 14 beta, Xcode Version 12.0 beta 6 (12A8189n) try to implement user Apple Login with Firebase. Works until password entering screen and then hangs up on simulator or finished with error on device. Sign In with Apple is enabled in App.
Any help with preview crash on an App that is building just fine and runs on simulator fine?
Are there logs that I can access somehow?
The previews were working just fine, but then stopped and I have reverted all changes since and the previews are still broken.
Hello,
I'm trying to remove the current back button behavior in a navigation view and instead have a simple arrow.
Disabling the button with navigationBarBackButtonHidden and then adding my own with navigationBarItems works, but then I lose the swipe gesture to go back. I could try to rebuild this swipe gesture, but that seems hard to get the same feel, and it will most likely break often.
Is there something simpler ?
I'd be satisfied with just removing the text of the back button and keeping only the <.
Thanks
Repro steps: Create new project
Create WidgetKit extension (verify that previews work)
Create dummy framework (verify that previews still work)
Link dummy framework to the widget extension
At that point, preview stops working with the following error:
"RemoteHumanReadableError
ProcessError: Failed to launch widget extension: The operation couldn’t be completed. (CHSErrorDomain error 1050.)"
What can I do to make this work?
I'm trying to make a gradient for Widgets and using 2 blue colors (#0091f1 and #0054f3) but the Widget background looks green.
I've set up colors via Assets.xcassets and then using the following code:
LinearGradient(gradient: Gradient(colors: [Color("color1"), Color("color2")]), startPoint: .top, endPoint: .bottom)
For the common iOS target the gradient looks right (blue) and for the Widgets extension it's green.
Could you help me to figure out why this is happening?
The link - https://github.com/maximbilan/SwiftUI-WidgetKit-Gradient-Issue to the example with screenshots.
Hey there!
Got a question about font kerning:
When adding a negative kerning to a text (changes via user input) the last character sometimes gets cut off:
dropbox.com/s/49ucdzk8m4k61sj/fontproblem1.png?dl=0
dropbox.com/s/vmklvxp510wjeak/fontproblem2.png?dl=0
What i do is the following:
Text("\(pos, specifier: "%.1f")")
	.font(.system(size: 100,design: .serif))
	.fontWeight(.bold)
	.kerning(-5)
When i remove the kerning it works, but as i understood the kerning keeps the letters as they are? Is there an alternative way of doing it?
I'm trying to create a custom Picker similar to the one in the Reminders app where users can select colors and icons for a list of reminders.
I currently have this:
struct CustomPickerStyle: PickerStyle {
static func _makeView<SelectionValue>(value: _GraphValue<_PickerValue<CustomPickerStyle, SelectionValue>>, inputs: _ViewInputs) -> _ViewOutputs where SelectionValue : Hashable {
}
static func _makeViewList<SelectionValue>(value: _GraphValue<_PickerValue<CustomPickerStyle, SelectionValue>>, inputs: _ViewListInputs) -> _ViewListOutputs where SelectionValue : Hashable {
}
}
These are automatically generated by Xcode. How should I deal with these methods? I could not find any documentation online for making a custom picker view...
Thanks!
Is it possible to present UIMenu programmatically in UIButton or UIBarButton? I have custom logic where I'd like to show that menu.
I couldn't find any solution.
I've come across some strange behavior with SwiftUI's onAppear() and onDisappear() events. I need to be able to reliably track when a view is visible to the user, disappears, and any other subsequent appear/disappear events (the use case is tracking impressions for mobile analytics).
I was hoping to leverage the onAppear() and onDisappear() events associated with swiftUI views, but I'm not seeing consistent behavior when using those events. The behavior can change depending on view modifiers as well as the simulator on which I run the app.
In the example code listed below, I would expect that when ItemListView2 appears, I would see the following printed out in the console:
button init
button appear
And on the iPhone 8 simulator, I see exactly that.
However, on an iPhone 12 simulator, I see:
button init
button appear
button disappear
button appear
Things get even weirder when I enable the listStyle view modifier:
button init
button appear
button disappear
button appear
button disappear
button appear
button appear
The iPhone 8, however remains consistent and produces the expected result.
I should also note that in no case, did the Button ever seem to disappear and re-appear to the eye.
These inconsistencies are also not simulator only issues, i noticed them on devices as well.
I need to reliably track these appear/disappear events. For example I'd need to know when a cell in a list appears (scrolled into view) or disappears (scrolled out of view) or when, say a user switches tabs.
Has anyone else noticed this behavior? To me this seems like a bug in SwiftUI, but I'm not certain as I've not used SwiftUI enough to trust myself to discern a programmer error from an SDK error. If any of you have noticed this, did you find a good work-around / fix?
Thanks,
Norm
// Sample code referenced in explanation
// Using Xcode Version 12.1 (12A7403) and iOS 14.1 for all simulators
import SwiftUI
struct ItemListView2: View {
		let items = ["Cell 1", "Cell 2", "Cell 3", "Cell 4"]
		var body: some View {
				ListingView(items: items)
		}
}
private struct ListingView: View {
		let items: [String]
		var body: some View {
				List {
						Section(
								footer:
										FooterButton()
										.onAppear { print("button appear") }
										.onDisappear { print("button disappear") }
						) {
								ForEach(items) { Text($0) }
						}
				}
//			.listStyle(GroupedListStyle())
		}
}
private struct FooterButton: View {
		init() {
				print("button init")
		}
		var body: some View {
				Button(action: {}) { Text("Button")	}
		}
}
Hi everyone! I recently noticed a strange behaviour with the new SwiftUI Toolbar.
I have an app with only 2 views: a ContentView and a DetailView. My goal is to have a different toolbar in both.
ContentView:
struct ContentView: View {
@State var isFavorite = false
var body: some View {
NavigationView {
VStack {
Image(systemName: isFavorite ? "star.fill" : "star")
NavigationLink(destination: DetailView(isFavorite: $isFavorite)) {
Text("Go")
}
}
.navigationTitle("ContentView")
.toolbar() {
ToolbarItem(placement: .bottomBar) {
Text("This is my ContentView")
}
}
}
}
}
DetailView:
struct DetailView: View {
@Binding var isFavorite: Bool
var body: some View {
Button("Tap me!") {
isFavorite.toggle()
}
.navigationTitle("DetailView")
.toolbar() {
ToolbarItem(placement: .bottomBar) {
Text("This is my DetailView")
}
ToolbarItem(placement: .navigationBarTrailing) {
Image(systemName: isFavorite ? "star.fill" : "star")
}
}
}
}
When I toggle the @Binding property in the DetailView, the toolbar is replaced by another empty toolbar appearing from nowhere.
Am I doing something wrong?
In our app, we display contacts in UITableView. Let us say I have 300 contacts in my AddressBook, and all of them will be displayed in this table.
Below this table, I have a UIButton to perform some action like invite selected contacts.
With Voice Over enabled, when I get to the UITableView, it doesn't let me to proceed to the UIButton, unless I go over all 300 contacts.
Is there a solution to override this and make it more friendly to the visually impaired users?
I'm getting this message in console:
[Assert] Attempted to call -cellForRowAtIndexPath: on the table view while it was in the process of updating its visible cells, which is not allowed. Make a symbolic breakpoint at UITableViewAlertForCellForRowAtIndexPathAccessDuringUpdate to catch this in the debugger and see what caused this to occur. Perhaps you are trying to ask the table view for a cell from inside a table view callback about a specific row? Table view: <UITableView: 0x10c04a000; frame = (0 0; 414 659); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x280262bb0>; layer = <CALayer: 0x280d302c0>; contentOffset: {0, 183.5}; contentSize: {414, 3068}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <MyApp.MyDataSource: 0x282930ea0>>
It happens when my table view is in editing mode, some cells are selected and are about to be reused (I'm scrolling the table and selected cells leave the visible area).
I don't call -cellForRowAtIndexPath: or visibleCells directly in my code.
Here's the stack trace when this happens:
Thread 1 Queue : com.apple.main-thread (serial)
#0 0x0000000185303650 in UITableViewAlertForCellForRowAtIndexPathAccessDuringUpdate ()
#1 0x0000000185303378 in -[UITableView _cellForRowAtIndexPath:usingPresentationValues:] ()
#2 0x00000001852f1bb0 in -[UITableView _updateSelectionGroupingForCell:atIndexPath:] ()
#3 0x00000001852f1e78 in -[UITableView _updateSelectionGroupingForVisibleCells] ()
#4 0x00000001852af9d0 in -[UITableViewCell showSelectedBackgroundView:animated:] ()
#5 0x00000001852b045c in -[UITableViewCell setSelected:animated:] ()
#6 0x00000001852adf5c in -[UITableViewCell prepareForReuse] ()
#7 0x000000018530b2b4 in -[UITableView _dequeueReusableViewOfType:withIdentifier:] ()
#8 0x000000018530b908 in -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:] ()
#9 0x000000018530b764 in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] ()
#10 0x000000010103a4cc in MyDataSource.tableView(_:cellForRowAt:) at /../MyDataSource.swift:87
#11 0x000000010103a9f4 in @objc MyDataSource.tableView(_:cellForRowAt:) ()
#12 0x00000001853241ec in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] ()
#13 0x00000001852eff84 in -[UITableView _updateVisibleCellsNow:] ()
#14 0x000000018530e394 in -[UITableView layoutSubviews] ()
#15 0x0000000185643d90 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#16 0x0000000185b59384 in -[CALayer layoutSublayers] ()
#17 0x0000000185b59858 in CA::Layer::layout_if_needed(CA::Transaction*) ()
#18 0x0000000185b6dff8 in CA::Layer::layout_and_display_if_needed(CA::Transaction*) ()
#19 0x0000000185ab3ee0 in CA::Context::commit_transaction(CA::Transaction*, double, double*) ()
#20 0x0000000185adf36c in CA::Transaction::commit() ()
#21 0x0000000185ae06fc in CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) ()
#22 0x00000001826c3358 in CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION ()
#23 0x00000001826bd5c4 in __CFRunLoopDoObservers ()
#24 0x00000001826bdb74 in __CFRunLoopRun ()
#25 0x00000001826bd21c in CFRunLoopRunSpecific ()
#26 0x000000019973c784 in GSEventRunModal ()
#27 0x00000001850f6200 in -[UIApplication _run] ()
#28 0x00000001850fba74 in UIApplicationMain ()
#29 0x000000010111a824 in main at /.../MyApp/AppDelegate.swift:17
#30 0x000000018237d6c0 in start ()
This only happens in iOS 14.2, no such problem on iOS 13.7.
Does it look like an UIKit bug? Does anyone know how to solve it?