0

I've added a label in a view controller via the interface builder, and I've added a lot of constraints to it, but I want to replace it by a button. Can I accomplish this without losing all the constraints ? Thanks a lot in advance

Xys
  • 8,486
  • 2
  • 38
  • 56

3 Answers3

1

Don’t replace by button

Add tap gesture for click action

Kathiresan Murugan
  • 2,783
  • 3
  • 23
  • 44
1

I'm not sure if you can save your constraints inside interface builder. However, you can add a tap gesture recognizer to make the label perform an action when it is tapped (act like a button).

This code can help you get started:

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
myLabel.isUserInteractionEnabled = true
myLabel.addGestureRecognizer(tapGestureRecognizer)


func handleTap(_ sender: UITapGestureRecognizer) {
    // perform some action when the label is tapped
}

You can look at this question for more information.

DoesData
  • 6,594
  • 3
  • 39
  • 62
-1

you don't need to remove that label. Just addGestureRecognizer a tap gesture on label.

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(_ :)))
myLabel.isUserInteractionEnabled = true
myLabel.addGestureRecognizer(tapGestureRecognizer)


func handleTap(_ sender: UITapGestureRecognizer) {
    // perform some action when the label is tapped
} 
Pradeep Singh
  • 150
  • 2
  • 5