Swiftui onFocus is deprecated in xcode 13.5 beta, how to solve? - swiftui

.onFocus({ isFocused in
variable = false
})
This code is not working anymore since update to 13.5 beta.
How can i achieve the same, so if a textfield is focused then....do something

Related

On iOS 15, the UIHostingController is adding some weird extra padding to its hosting SwiftUI view (_UIHostingView)

UPDATE: 2022-09-26
This issue has been fixed on iOS 16. Although the issue is still present on iOS 15 even when the project is compiled with the iOS 16 SDK.
Original question:
On iOS 15, the UIHostingController is adding some weird extra padding to its hosting SwiftUI view (_UIHostingView).
See screenshot below (Blue = extra space, Red = actual view’s):
Does anyone know why this happens?
I've reported this bug, Apple folks: FB9641883
PD: I have a working project reproducing the issue which I attached to the Feedback Assistant issue. If anyone wants it I can upload it too.
I found out that subclassing UIHostingController as follows fixes the issue with extra padding:
final class HostingController<Content: View>: UIHostingController<Content> {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
view.setNeedsUpdateConstraints()
}
}
It also fixes a problem of the UIHostingController not resizing correctly when its SwiftUI View changes size.
I’ve tried to find why is this happening without luck. The only thing I’ve found to fix it is setting a height constraint to its intrinsic content size in a subclass of UIHostingController:
private var heightConstraint: NSLayoutConstraint?
override open func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 15.0, *) {
heightConstraint = view.heightAnchor.constraint(equalToConstant: view.intrinsicContentSize.height)
NSLayoutConstraint.activate([
heightConstraint!,
])
}
}
override open func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
heightConstraint?.constant = view.intrinsicContentSize.height
}

How can I use navigationBarTitleDisplayMode on watchOS 7 and below?

When I try to use navigationBarTitleDisplayMode in my project targeting watchOS 6, I get this error:
'navigationBarTitleDisplayMode' is only available in application extensions for watchOS 8.0 or newer
How can I use it in earlier versions of watchOS? I know it won't have an effect there, because the style doesn't exist, I just want to circumvent the error.
You can do it with a ViewBuilder extension:
extension View {
#ViewBuilder
func navBarTitleDisplayMode(_ mode: NavigationBarItem.TitleDisplayMode) -> some View {
if #available(watchOSApplicationExtension 8.0, *) {
self
.navigationBarTitleDisplayMode(mode)
} else {
self
}
}
}
Usage:
someView
.navigationBarTitle("WatchFunk") // Using this for watchOS 6 compatibility.
// Use navigationTitle when targeting
// watchOS 7 and above.
.navBarTitleDisplayMode(.inline)

iOS 14 AssistiveTouch Dwell Control sometimes not working on SwiftUI Buttons

I have a SwiftUI App which uses regular Button views and our customers use AssistiveTouch's DwellControl feature quite often. On iOS 13 DwellControl was working fine but on iOS 14 beta 8 DwellControl fails to be able click on those buttons.
The Buttons can be tapped by finger and clicked with the normal mouse but not with the DwellControl feature.
I have tried adding accessibility traits like isButton, but nothing works.
Has anyone encountered the same problem and has a solution or any insight at all? I know this is quite niche but any help is appreciated!
EDIT:
I have tested it with a very simple example, including one a Button and it works fine. It seems to be a side effect of some sorts in my specific App.
I have several ZStacks which show and hide some views like modals and popovers. Could this be the source of failure?
What I don't get is that the Buttons can be tapped and clicked... If a view was blocking the Button then this shouldn't be possible right?
I have tried Release and Debug builds which does not make a difference.
When DwellControl is activated and the cursor is on the Button that shall be clicked, tapping doesn't work either. But when the cursor is nowhere near the Button, tapping works fine.
I have send a report via Feedback to Apple.
EDIT:
I found the cause. ScrollView somehow prevents all DwellControl clicks from happening. That happens whenever a ScrollView is somewhere present in one of the Views.
Minimal example:
struct ContentView: View {
#State var show = false
var body: some View {
ZStack {
ScrollView {
Button(action: {
print("This should be printed but isn't....")
}, label: {
Text("Button in ScrollView")
})
}
}
}
}
I know its niche but maybe someone needs this.
There are other components which are affected and cause the same behaviour:
ScrollView
TextField
Slider
Stepper
UIViewRepresentable
For me this is a big issue, since our customers use DwellControl quite often.
EDIT:
This can be replicated in the newest iOS 14.2 beta 1:
import SwiftUI
#main
struct TempSwitUIApp: App {
#State private var text: String = "I am a text"
var body: some Scene {
WindowGroup {
Button(action: {
print("\(UUID()) \(self.text)")
}, label: {
Text("Print text to the console")
})
TextField("Hello", text: self.$text)
}
}
}
I updated my issue with Apple but haven't heard from them yet.

SwiftUI iOS 14 beta TextField 100% CPU

Using iOS 14 and Xcode 12.0 beta 6 if I try and use a simple TextField anywhere
import SwiftUI
struct ContentView: View {
#State private var name: String = "Tim"
var body: some View {
VStack {
TextField("Enter your name", text: $name)
Text("Hello, \(name)!")
}
}
}
the keyboard opens but then the CPU goes to 99%/100% and app is frozen.
Is this a known issue? How do I fix it?
This bug exists since the 14.0 betas and has not been fixed so far :/ I tried to search for workarounds or solutions but there seems to be none at the moment.
Once the user activates an input field, the CPU goes to 95%-100% and stays there until you actually quit the app.
I found some reason, If you use some .onAppear listener, When device keyboard is opened, application is being crazy if you set or change any #EnvironmentObject variable using .onAppear listener anywhere on your app. But it is not for all .onAppear... it was really weird. I searched piece by piece those when I have noticed.

Adding WKInterfaceTimer to SwiftUI

How to add WKInterfaceTimer to SwiftUI ? Anyone who already did it ?
There is no init for WKInterfaceTimer how to do it ?
Please try:
Text(date, style: .timer)
The style .timer ensures that the text will be updated automatically.
Please note: This feature has been added to iOS 14 and watchOS 7.