SwiftUI darken modifier - swiftui

SwiftUI has a helpful ViewModifier called brighten, does anyone know how to do the opposite and darken a view? I'd hoped passing in a negative value would do the trick, but that doesn't work.

For darken the View, You can give negative value to brightness modifier like this,
MyView().brightness(-0.5)

Related

SwiftUI: Status bar color for specific view

I have a specific view in my application where I need the status bar to be white (dark mode).
I've tried setting the .preferredColorScheme(.dark) but as the documentation mentions, that affects all the views in my window which is not what I'm looking for.
I've taken a look at this but it seems to be done using SceneDelgate which I am not making use of.
Is there any workaround to this?
For iOS 16, you can set .toolbarColorScheme for a specific View. Unfortunately, this is not supported on older iOS Versions. As a workaround that does not involve SceneDelegate, you could manipulate UIToolBar.appearance() directly with a new tint color. This has nothing to do with the preferred color scheme (e.g., light or dark mode), but could have the same effect.

Detect single/multi-finger gesture Swiftui

How to detect number of finger interaction with Gesture in SwiftUI Gesture. With UIKit we can find number of find it from
let numberOfTouches = panGestureRecognizer.numberOfTouches
Any thought?
I am able to find my solution using UIKit UIView. Actually made container view(UIVIewRepresentable) which implements the PanGesture. I have used this view to to hold SwiftUI view. I can easily find and able to handle Panning behaviour.

ScrollViewReader scrollTo with .center anchor bug?

So I'm try to use ScrollViewReader to programmatically scroll a horizontal scroll view. I thought it would work like scrollToItem with .centeredHorizontally in UIKit, and for the most part it does, but the last few elements in the scroll view are being forcefully scrolled to the center of the screen, despite the fact that the scroll view isn't normally able to scroll that far over (without snapping back after releasing the drag, at least). This ends up creating white space across the trailing half of the screen.
I've seen some other questions about this and it seems like the general opinion is that it's not a bug? On the one hand I suppose we're telling the scroll view to center the item, and it's doing just that -- so, not a bug? On the other hand, that's not how similar functionality worked in UIKit. Also, this behavior is only happening on the trailing end of the scroll view! If it was the intended behavior I would expect that scrolling to the first element in the scroll view using .center anchor would force it into the center of the screen and leave leading white space, but this doesn't happen.
Is there an elegant solution to this? Or do we have to calculate the width of our elements + spacing and figure out based on the screen width whether we should anchor .center or just scroll to the last element with anchor .trailing in order to replicate the UIKit behavior?
I found a package (Amzd/ScrollViewProxy) that was made before ScrollViewReader was released that functions much the same as ScrollViewReader, but also seems to not have the bug (if it is a bug) detailed in the question.
Usage examples can be seen on the repository page, but here's a quick minimal example.
ScrollView(.horizontal) { scrollProxy in
ForEach(sections) { section in
Text(section.text)
.scrollId(section.id)
}
.onChange(of: index) {
scrollProxy.scrollTo(
sections[index].id,
alignment: .center
)
}
}
The package adds a convenience init to ScrollView to give access to the scrollProxy.
I can confirm this behavior and think it should be considered a bug. Especially since the scroll view will "jump" into position on the first touch event.
As of iOS 15.4 Beta 1 this is fixed for me. Maybe give it another try.

How do I set a view's background to the gray'ish one in SwiftUI?

I noticed that whenever I create a view it automatically gets a white background color. If you insert a list however, no matter the size, the entire view seems to get a sort of grey background, which looks better to me than a white background. I'm wondering how I can get my background to look like this (without using a list, or an invisible dummy list maybe)? I've already tried getting the color code but that just gives me a dark grey background. I tried adjusting the opacity but it just isn't the same as the native one.
edit: It appears the size of the list does matter, it seems to have something to do with NavigationViews and NavigationLinks
That is the default group table view background color (systemGroupedBackground) with the color code #F2F2F7. You can use it as a background color like this:
.background(Color(red: 0.949, green: 0.949, blue: 0.97, opacity: 1.0))
...or simply:
.background(Color(.systemGroupedBackground))
Keep in mind that with the latter solution, the color might change in later iOS versions if Apple decides to change the value of the constant.

Famo.us Scrollview setPosition with transition

Trying to very simply scroll a famous Scrollview to a pixel position with a transition. It looks like Scrollview's setPosition function will set the scroll position in pixels (right?). But how do I add a transition to this.
My first thought was to use a Transitionable to move from the scrollview's getPosition to the end position, but this led to another question: How do I watch a transitionable?
Thanks!
Right now the famous Scrollview does not support setting the position with any kind of transition. There are a number of things you can do to simulate the transition, such as dividing the total difference in position by the length of the transition and then adding that value to the position every render frame. This would give the illusion of a transition.