I created a control which group a TextField, a Text as the label of the TextField, and eventually a Text to signal an error
The result will be something like that:
To declare this control, I write the following Code:
struct TextFieldWithError<T: Hashable>: View {
var label: String
@Binding var text: String
@Binding var textError: String
var focusId: T?
@FocusState.Binding var focus: T?
var body: some View {
VStack {
HStack {
if layout == .hstack {
Text(label)
}
if let focusId {
TextField(label, text: $text)
.focused($focus, equals: focusId)
} else {
TextField(label, text: $text)
.modifier(TextFieldStyle())
}
}
if textError != "" {
Spacer()
.frame(height:2)
HStack {
Text(textError)
.foregroundColor(.red)
.frame(height: textError == "" ? 0 : 20)
.animation(.default)
}
}
}
}
}
if I use a generic for my control, it's because I want to control focus with the .focused modifier and I didn't found another way to do this
now, I want to control the width of the label with a modifier.
I thought about writing
.TextFieldWithErrorFrame(labelWidth: <some CGFloatvalue>)
struct TextFieldWithErrorFrame: ViewModifier {
varlabelWidth: CGFloat
func body(content: Content) -> some View {
content
}
}
I think that in place of writing content in the body of my modifier, I have to write the same code that I wrote for the control body, applying a frame modifier on the Label.
But I don't find the way of doing this.
if I declare the modifier as this:
struct TextFieldWithErrorFrame<T>: ViewModifier where T: Hashable {
var width: CGFloat
func body(content: TextFieldWithError<T>) -> some View {
content
}
}
I need to declare an type alias for the body, because I have the error:
Type 'TextFieldWithErrorFrame' does not conform to protocol 'ViewModifier'
if I use
typealias Body = TextFieldWithErrorFrame<T>
I have the same error
and that's not all.
how to rewrite the content in the body of the modifier?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
For my app working on iPhone and IPad, I have two different designs depending on the landscape or portrait orientation.
But it's not so simple, because for iPad, the design is the same while in portrait or landscape orientation.
So I thought to user verticalSizeClass. If it is compact, I apply portrait design, if it is Regular, I apply landscape design.
So I introduce the environment variable:
@Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?
And create the following modifier (found on Apple Developper Forum)
struct DeviceRotationViewModifier: ViewModifier {
let action: (UIDeviceOrientation) -> Void
func body(content: Content) -> some View {
content
.onAppear()
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
action(UIDevice.current.orientation)
}
}
}
// A View wrapper to make the modifier easier to use
extension View {
func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
self.modifier(DeviceRotationViewModifier(action: action))
}
}
Implementing the OnRotate modifier on my contentView
.onRotate { newOrientation in
print(verticalSizeClass)
}
I find that the verticalSizeClass is not correct on first call of OnRotate (it's always regular).
After when I change orientation, the verticalsizeClass becomes correct.
What did I missed?
I develop an App for Mac and iPhone, and till now, I had no issue to test it on my iPhone.
but this morning, I have the following message, when I try to run it on my iPhone:
Failed to verify code signature .... (A valid provisioning profile for this executable was not found.)
Verify that the Developer App certificate for your account is trusted on your device. Open Settings on the device and navigate to General -> VPN & Device Management, then select your Developer App certificate to trust it.
I must precise that it works on the simulator.
the version of Xcode is 15.2 and the version of iPhone is 17.2.1
when I go on Settings/VPN -> Device Management (on iPhone), I don't see any section for Developper App Certificate
when I go to Devices and Simulators on Xcode, and list the Provisioning Profiles installed on my iPhone, I see the IOS Team Provisioning Profiled of my application
but it still not work.
What can I do?