1

How can I modify a View after using it? E.G:

var body: some View {
    Button (action: {
        // Code to move button's position. self.offset(x:y:) doesn't work, Xcode says 'result of call to offset is unused'
    }) {
        Text("How to I alter the colour of this text?") // Change colour somewhere else in my script
            .background(Color.black)

    }
        .frame(alignment: .leading)

}

I need to modify a View after it has been instantiated. Because everything is a View, from the .frame to the .background, shouldn't I need to reference/delete/modify/add them to the stack?

3
  • Just a random guess, can we use var button instead of body and then body.color like something? Can you please check this? Commented Jun 12, 2019 at 12:45
  • 1
    @Rob this is SwiftUI. It is a whole new way of creating views. Commented Jun 12, 2019 at 12:53
  • I know that's why I have said a random guess. Commented Jun 12, 2019 at 13:08

2 Answers 2

7

A change of color is a change of state.

You can use the property wrapper @State, which is part of SwiftUI.

More about this in the excellent WWDC 2019 talk:

Introducing SwiftUI: Building Your First App

struct ContentView: View {

    @State var someState: Bool = true

    var body: some View {
        Button (action: {
            // The state is toggled, and a redraw is triggered
            self.someState.toggle()
        }) {
        Text("How do I alter the colour of this text?")
            // Set color according to the state
            .foregroundColor(someState ? Color.black : Color.pink)
        }
        .frame(alignment: .leading)
    }

}

When a change of @State occurs, the view body is redrawn.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That was a real facepalm moment for me. I completely forgot about @State 😂😂
3

SwiftUI requires a complete change of thought patterns compared to using UIKit.

The "view" you are referring to is not actually a view. It is a set of instructions of how to render something onto the screen. So you can't access it with a reference etc...

To change the way a view looks you can use modifiers on it as you are creating it.

For instance...

Button(action: {}) {
    Text("Hello world!")
        .foregroundColor(.yellow)
}

It really depends what you want to do with it.

Having said that. It is clear that you haven't spent any time watching the WWDC videos or going through the SwiftUI tutorials.

I would recommend doing these before you go any further.

https://developer.apple.com/tutorials/swiftui/

2 Comments

I'll have you know that I have been watching the keynotes live, and have been reading Paul Hudson's guides since they came out. No tutorial is new on me
Well put. Although to nitpick a bit: SwiftUI views aren’t instructions for how to render the screen, they’re a description of what you want rendered. The “how” part gets figured out automatically by ~~an avocado juggler~~ the framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.