I have a published property in SwiftUI declared thus:
import SwiftUI
class UserData: ObservableObject {
#Published var showLoginButton = true
In the debugger at a breakpoint I cannot see the value of this property. I can see the value of properties that are not decorated as #Published.
What expression do I need to see the current value of a published property when debugging SwiftUI?
#Published is a property wrapper, so you see its storage. To get value just print property, so here it is
(lldb) e _showLoginButton
(Published<Bool>) $R0 = {}
(lldb) e self.showLoginButton
(Bool) $R2 = true
Make sure your source code (tests included) includes import Combine. In the Xcode debugger you can then use the print description button (icon is a circle with an 'i' in the center) to view the current value. Without the import the console will show "error: cannot find type 'Combine' in scope".
Related
I am having issues with localization text in SwiftUI. I created a file called "Localizable.string" which contains the following key and values.
"helloTitle" = "Hello!";
I use the localizedString as shown below:
var body: some View {
Text("helloTitle")
.padding()
}
Instead of display "Hello" it displays "helloTitle".
Here are my build settings:
For some reason Base has 0 Files Localized. I am not sure why?
Double check the target membership of your Localizable.strings file. It should be part of the same target as your view.
try renaming the file to Localizable.strings, note the "s"
So, I have few steps, last one contains EnvironmentObject and ObservedObject. The issue is, when I try to modify EnvironmentObject (lane 68) it re-creates ObservedObject.
Can any one explain me why this happens? Any solution to keep my ObservedObject with original state?
As far as I know it possible to change ObservedObject to StateObject, but I am using iOS 13+ so... I need other solution.
Line 47 - body is reevaluated so new instance of ObservedStuff is created, so make it as property and pass it in, like
struct TestView_A: View {
...
private let model = ObservedStuff()
var body: some View {
NavigationLink(destination: TestView_B(viewModel: self.model) ...
}
}
When specifying a minimumScaleFactor for a TextField in SwiftUI the TextField behaves normally while you enter text and reduces the font as specified when the content does not fit the TextView. However, if you start deleting characters everything works as usual until you delete the first character. Everything freezes.
At the beginning I though it was something in the way I was handling the variable that stores the text that in my application I have it as an ObservedObject. However, after debugging the frozen app I noticed that the code was circling around the drawing of the TextField over and over, function after function everything pointed to an error in the drawing of the object on the screen.
The following code illustrates the issue. The TextField works perfectly when you enter characters and delete them until you get to the first one. The it freezes.
import SwiftUI
struct ContentView: View {
#State var sensorNumber: String = ""
var body: some View {
TextField("WC0.000.000.000", text: $sensorNumber)
.padding(.all, 5.0)
.font(Font.custom("Helvetica", size:40.0))
.minimumScaleFactor(0.90)
}
}
The problem seems to be related to the interaction of the Custom Font. Obviously, my application is using custom fonts but here I just wanted to simplify the code.
This code does not fail if you don't use a custom font or if you don't specify a minimumScaleFactor. I have found a workaround that is not very elegant but it works until Apple fixes this bug:
import SwiftUI
struct ContentView: View {
#State var sensorNumber: String = ""
var body: some View {
TextField("WC0.000.000.000", text: $sensorNumber)
.padding(.all, 5.0)
.font(Font.custom("Helvetica", size:40.0))
.minimumScaleFactor(sensorNumber.count < 2 ? 1.0 : 0.90)
}
}
I am submitting a radar to Apple but looking for a better solution for the problem here.
I'm trying to use this library: soffes/HotKey in an SwiftUI Application for MacOS.
The author describes the usage as follows:
Set up Hotkey:
let hotKey = HotKey(key: .r, modifiers: [.command, .option])
set the keyDownHandler and get callbacks for when your hot key is pressed:
hotKey.keyDownHandler = {
print("Pressed at \(Date())")
}
My problem is that I don't know where I have to put the Handler. Any Ideas?
I ran into this same problem; I was initializing a hotkey inside my appDelegate's applicationDidFinishLaunching function. I had it declared locally and I think it was getting garbage collected.
The fix for me was to make a hotKey property on my appDelegate (although I'm sure you could do the same on any root view or scene if you aren't using an appDelegate) and assign my hotKey to it:
class AppDelegate: NSObject, NSApplicationDelegate {
var hotKey: HotKey?
func applicationDidFinishLaunching(_ aNotification: Notification) {
let hotKey = HotKey(key: .six, modifiers: [.command, .option])
hotKey.keyDownHandler = {
print("Got hotkey at \(Date())")
}
self.hotKey = hotKey
}
}
Okay so I figured it out.
The keyDownHandlerreally just defines what should happen when a hotkey is pressed so you can just put it into the .onAppear {} after your body View
I have the following set up for my UIToolBar / Accessory View on a view controller
#IBOutlet var inputFieldView: UIToolbar!
override var canBecomeFirstResponder: Bool{
return true
}
override var inputAccessoryView: UIView?{
return self.inputFieldView
}
then inside my viewDidLoad I have:
let seperator = UIView(frame: CGRect(x:0 , y: 0, width: ScreenSize.width(), height: 1))
seperator.backgroundColor = UIColor.lightBackground
self.inputFieldView.addSubview(seperator)
self.inputFieldView.isTranslucent = false
self.inputFieldView.setShadowImage(UIImage(), forToolbarPosition: .any)
self.inputFieldView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
self.inputFieldView.removeFromSuperview()
This worked great for ios versions 10 and 9. It is a text view with a "Send" button. It sits at the bottom of the screen and when pressed, becomes first responder allowing the keyboard to come up and it positions itself correctly.
With ios 11 i cannot even click on it when it is at the bottom, so I cannot type at all.
This is a known bug on iOS 11. UIToolbar subviews don't get the touch events because some internal views to the toolbar aren't properly setup.
The current workaround is to call toolBar.layoutIfNeeded() right before adding subviews.
In your case:
inputFieldView.layoutIfNeeded()
Hopefully this will get fixed on the next major version.
Solved it. It looks like UIToolbar's just are not working correctly in iOS 11.
Changed it to an UIView and removed
self.inputFieldView.isTranslucent = false
self.inputFieldView.setShadowImage(UIImage(), forToolbarPosition: .any)
self.inputFieldView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
got it working (and changed it to a UIView from UIToolbar in the xib as well.)