I am implementing timer on lock screen widget using Timer.publish and .onReceive on Text. But the timer is not firing. let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
I have a Text() in ZStack which I want to update.
Widgets are static, your app sends an encoding of the View structs to the lock screen so none of the actions work. However, there are ways to request a refresh, see Keeping a Widget Up To Date | Apple Developer Documentation
Related
I am trying to programatically change the Apple Watch StatusBar Clock Text Color in my WatchOS only app written in SwiftUI.
I've tried
.preferredColorScheme(.dark)
.preferredColorScheme(.light)
on var body: some View but that does not seem to take effect. Am I missing a step?
In my widget, I place a header view.
var header: some View {
ZStack {
Link(destination: url) {
Rectangle().fill(Color("WidgetBackground")).frame(height: 36)
Text("Header")
}
}
}
This is a simple text on a colored background.
The url is the urlscheme for my app.
When I tap on the label or on header view, it opens my app on medium and large size widgets.
But on the small size widgets, it didnt open the app, but it flickers and reload the widget.
I also see the flicker happen in medium and large size widgets when I tap on empty spaces other than elements.
I have no clue , why this flicker happens.
Based on documentation, interacting on widget should open the app, without any extra effort.
Am I missing something ?
I don’t know why the app doesn’t open (an iOS bug, I guess), but your deeplink won’t work because Link isn’t available in the “small” widget size. You need to set the .widgetURL() modifier on your view instead.
Is there any way to customize the gesture recognizer used by .onDrag() in a SwiftUI View? The developer documentation states "applying the onDrag(_:) modifier adds the appropriate gestures for drag and drop to this view" but is silent, so far as I can see, as to how to alter the behavior of those gestures. Those gestures wait for a longpress before initiating the drag. I would like to reduce that delay to zero.
Why Required
The app currently uses a custom DragGesture and .offset(value) to effect a drag. This strategy requires that the view in which the drag initiates have a greater .zIndex than any view over which an item might be dragged. Since drags can begin in different views, the .zIndex for each view is managed programmatically through ternary operators.
The .onDrag() functionality puts the dragged item on top of all views regardless of .zIndex. This behavior is now required due to implementation of a magnification gesture, which requires that the magnified view have a .zIndex below that of the other views or it will cover them as it expands. If the magnified view is then the source of a drag, the required .zIndex behaviors (high for drag, low for magnification) are incompatible.
I tried using .clipped() on the magnified view, but that prevents the dragged item from appearing outside of that view.
Apple developer support responds that there is no way to customize the gesture recognizer automatically added by SwiftUI to .onDrag()
After adding a combined gesture to a view, a TextField inside the view would no longer respond when I would tap into it to change the text. I discovered this after adding a custom combined gesture - where I used a long press to start things before dragging. (Note: things still worked if just a drag gesture was added. Not sure what is particularly different between these two cases.)
The combined gesture:
let combined = longPressGesture.simultaneously(with: dragGesture)
The gesture was added to the view with:
.gesture(combined)
I got things to work by adding an onTapGesture{} to the TextField. Didn’t have to put anything into the action. Seems like a side effect whose behavior could change in the future. Appreciate any comments on if this makes sense or other ways to handle.
TextField(“Enter Text”, text: $myText)
.textFieldStyle(RoundedBorderTextFieldStyle())
.onTapGesture {}
In case one would have this issue with a drag gesture, you can set the minimumDistance. This would still register the tap on the textfield to edit it.
DragGesture(minimumDistance: 30, coordinateSpace: .global)
Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling
Has anyone been able to resize the content view for an iOS 7 UITabBarController? In previous versions it consisted of two views. A UITabBar and a UITransitionView. The transition view was the content view. However as of iOS 7 SDK the UITransitionView is fullscreen and setting its size does nothing.
I need to resize the content view as I want to display an ad above the UITabBar.
I managed to reach the actual content view on iOS 7 (inside the UIViewControllerWrapper) via this
UIView *viewToResize = [[transitionView.subviews[0] subviews] objectAtIndex:0];
WHen you then resize that view, you should obtain the same effect as resizing the UITransitionView.