So I’m following a tutorial, and we have to place buttons into our storyboard.
They have given us a bit of code (which I’m assuming customizes the button) which is the following:
Button(action: {
print("Button pressed")
}, label: {
Text("AC").frame(width: 80, height: 80, alignment: .center)
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(40)
.font(.title)
})
I’m not sure where to put this, in the view controller? When I do I get an error. Also, how would I connect this code to the button in the story board?
Thanks for the help!
ajn
3
Answers
You can set up the Button outlet like this
and then create function which will be called when you tap on it
and in viewDidLoad
Create an IBOutlet for the button:
By assigning the button as an outlet, the button can later be used in storyboard. Now in ViewDidLoad():
You cannot use SwiftUI view as a UIKit subview, so place it into another SwiftUI view and then you can host it using UIHostingController to add into traditional viewControllers hierarchy.
Or You can use UIButton instead.