iOS - 用按钮替换标签
iOS - Replace a label by a button
我已经通过界面生成器在视图控制器中添加了一个标签,并且向它添加了很多约束,但我想用一个按钮替换它。我可以在不失去所有约束的情况下完成这项工作吗?提前致谢
不要用按钮替换
为点击动作添加点击手势
我不确定您是否可以将约束保存在界面生成器中。但是,您可以添加一个点击手势识别器,使标签在被点击时执行一个动作(就像一个按钮)。
此代码可以帮助您入门:
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
}
您可以查看 了解更多信息。
您不需要删除该标签。只需在标签上添加 GestureRecognizer 一个点击手势即可。
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
}
我已经通过界面生成器在视图控制器中添加了一个标签,并且向它添加了很多约束,但我想用一个按钮替换它。我可以在不失去所有约束的情况下完成这项工作吗?提前致谢
不要用按钮替换
为点击动作添加点击手势
我不确定您是否可以将约束保存在界面生成器中。但是,您可以添加一个点击手势识别器,使标签在被点击时执行一个动作(就像一个按钮)。
此代码可以帮助您入门:
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
}
您可以查看
您不需要删除该标签。只需在标签上添加 GestureRecognizer 一个点击手势即可。
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
}