Questions tagged [viewmodifier]
59 questions
2
votes
1 answer
Implementing a custom ViewModifier where output is conditional on concrete View type (SwiftUI)
I would like to create a ViewModifier where the output is conditional on the type of content it is modifying.
The best test of the concept I've managed (using Text and TextField as example View types) is as follows:
struct CustomModifier:…

rustproofFish
- 931
- 10
- 32
2
votes
2 answers
In SwiftUI, is there a way to preserve the type when applying a view modifier?
My question is simply as the title states. I want to take some View, apply a view modifier to it, and still be able to keep it as that original type instead of it becoming a some View.
Say we have a simple view like this:
struct SomeView: View {
…

Tim Fuqua
- 1,635
- 1
- 16
- 25
2
votes
2 answers
Why view modifier can't take in immutable variable?
I made a button modifier, the background colour should change according to if it has been clicked or not. But I get complaint that the input parameter of viewModifier is immutable.
I got error on btnBkColor of
.modifier(ActionButtonType(bkColor:…

Neocoleoidea
- 133
- 1
- 7
2
votes
2 answers
Swift Compiler Error: "Type 'Watermark' does not conform to protocol 'ViewModifier'"
I want to add a ViewModifier, as explained in the following tutorial by Paul Hudson (https://www.hackingwithswift.com/books/ios-swiftui/custom-modifiers).
My ViewModifier is:
import SwiftUI
struct Watermark: ViewModifier {
var text: String
…

Jonas Deichelmann
- 3,513
- 1
- 30
- 45
1
vote
0 answers
where this white spot come out from my swiftUi view?
I'm using a view modifier in my Form view in order to push up the view when the keyboard appears to avoid the keyboard hiding the text field, it used to work perfectly but I don't understand why suddenly when the keyboard appears there is a strange…

Damiano Miazzi
- 1,514
- 1
- 16
- 38
1
vote
1 answer
SwiftUI compiler directives trigger "Cannot infer contextual base" error for following statements
I have a SwiftUI View for a watchOS / iOS app in which I use the #if os(xxxOS) compiler directive to select between a watchOS specific View or an iOS specific View to provide the content for the the OSCommonView body var. Both the watchOS and iOS…

djmlewis
- 273
- 4
- 7
1
vote
2 answers
How to display an Error Alert in SwiftUI?
Setup:
I have a SwiftUI View that can present alerts. The alerts are provided by an AlertManager singleton by setting title and/or message of its published property @Published var nextAlertMessage = ErrorMessage(title: nil, message: nil). The View…

Reinhard Männer
- 14,022
- 5
- 54
- 116
1
vote
1 answer
iOS 16 View Modifier not consistent across List headers
Since iOS changed List to use UICollectionView instead of UITableView, all my Section headers have taken on the background colors of my list cells.
I use a ThemedList to color my lists throughout the app:
struct ThemedList: View where…

Darren
- 10,182
- 20
- 95
- 162
1
vote
1 answer
Implementing a custom ViewModifier that wraps an existing SwiftUI ViewModifier (.focused)
My app collects a significant amount of data via a form that comprises text entry controls and Pickers.
In order to improve UX I would like to track the current user focus within the form to allow UI feedback (e.g. highlighting the currently focused…

rustproofFish
- 931
- 10
- 32
1
vote
1 answer
View Modifier messing with animations
I've been looking to add a loading indicator to my project, and found a really cool animation here. To make it easier to use, I wanted to incorporate it into a view modifier to put it on top of the current view. However, when I do so, it doesn't…

greatxp117
- 57
- 11
1
vote
0 answers
SwiftUI recreate toolbar modifier
I try to recreate the .toolbar modifier Apple uses for their NavigationView. I created an own implementation of a NavigationStackView but also want to use a .toolbar modifier.
I got something to work using environment objects and custom view…

Manu
- 922
- 6
- 16
1
vote
0 answers
How to to add Blur overlay on top of all views using .Blur as ViewModifier in SwiftUI
I have created a viewModifier which blurs a view when added.
Problem is when I add it on parent view of all the contents, all views are blurred differently.
I assume it's because it goes to all the contents and blur each of them individually instead…

Dreiohc
- 317
- 2
- 12
1
vote
2 answers
SwiftUI .onAppear withAnimation speeds up each time the view appears. Why?
I have on-going animations in my app that are triggered on onAppear and configured using withAnimation, updating a @State property.
Each time the view appears, the animation runs a little faster than before, so if the view is displayed, then covered…

AVS
- 373
- 3
- 19
1
vote
0 answers
How to access safeAreaInsets of another CoordinateSpace in SwiftUI
I'm trying to write a view extension with a ViewModifier that allows me to position any view in the CoordinateSpace of another. My code so far does work, except that it has alignment issues if one of the CoordinateSpaces has a safe area.
I'm able to…

Marco Boerner
- 1,243
- 1
- 11
- 34
1
vote
0 answers
SwiftUI ERR: "Argument 'title' must precede argument 'style'
I'm working on a package that has many custom parameters with default values. Given that there are so many potential initializers it can quickly become confusing to properly order everything when you change many parameters.
I have a two part…

Alex Fine
- 139
- 1
- 9