为UILabel上的”Show Hide”文本的高度约束无法按预期工作

huangapple go评论59阅读模式
英文:

Show Hide for UILabel over constraint not working as expected for height constraint

问题

以下是您要翻译的代码部分:

I have `UILabel` which I am showing and hiding on `tapGesture`.
I have created label programatically and set constraint programatically only.

by default UILabel is visible with `heightConstraint = 230`
on tapGesture method I am making `heightConstraint = 0` and its hiding label correctly.
Again on tapGesture I need to show label,
so when I tap again, I can see `heightConstraint = 230` again, but label is not seen in UI.

What might be issue, only hide is working and show is not working.

Following is the code for tapGesture method where I am setting height constraints.

@objc func tapLabel(gesture: UITapGestureRecognizer) {
        print("tap working")
        isExpanded.toggle()
        if(isExpanded){
            NSLayoutConstraint.activate([
                reminderBulletsLbl.heightAnchor.constraint(equalToConstant: 230)
            ])
        }else{
            NSLayoutConstraint.activate([
                reminderBulletsLbl.heightAnchor.constraint(equalToConstant: 0)
            ])
        }
    }

我已翻译所需的代码部分,不包括其他内容。

英文:

I have UILabel which I am showing and hiding on tapGesture.
I have created label programatically and set constraint programatically only.

by default UILabel is visible with heightConstraint = 230
on tapGesture method I am making heightConstraint = 0 and its hiding label correctly.
Again on tapGesture I need to show label,
so when I tap again, I can see heightConstraint = 230 again, but label is not seen in UI.

What might be issue, only hide is working and show is not working.

Following is the code for tapGesture method where I am setting height constraints.

@objc func tapLabel(gesture: UITapGestureRecognizer) {
        print("tap working")
        isExpanded.toggle()
        if(isExpanded){
            NSLayoutConstraint.activate([
                reminderBulletsLbl.heightAnchor.constraint(equalToConstant: 230)
            ])
        }else{
            NSLayoutConstraint.activate([
                reminderBulletsLbl.heightAnchor.constraint(equalToConstant: 0)
            ])
        }
    }

I tried to deactivate the constraint but not working.

Please find the screen shot attached.

  1. Default state when label is seen

为UILabel上的”Show Hide”文本的高度约束无法按预期工作

  1. On Click, when label is hidden.

为UILabel上的”Show Hide”文本的高度约束无法按预期工作

now after clicking again label is not showing.

Thanks in advance.

答案1

得分: 0

首先,你需要保持高度约束的实例,不要每次都创建新的高度约束。像这样:

var constraint: NSLayoutConstraint?

viewDidLoad 方法中添加以下代码:

constraint = label.heightAnchor.constraint(equalToConstant: 250)
constraint?.isActive = true

然后,在 tapLabel 函数中添加以下代码:

constraint?.constant = isExpanded ? 20 : 250

第二,当你把高度设为0时,就无法再点击它,因为标签的高度为零,那么如果高度为零,又怎么能点击呢?如果你把高度设为20,就像我提供的例子一样,标签将是可点击的。如果你想完全隐藏标签,也许你需要采取另一种方法,而不是点击标签。

英文:

First you need to keep an instance of the height constraint don't create new height constraint every time. like this

var constraint: NSLayoutConstraint?

and inside the viewDidLoad add this

constraint = label.heightAnchor.constraint(equalToConstant: 250)
constraint?.isActive = true

then inside tapLabel function add

constraint?.constant = isExpanded ? 20 : 250

Second the most important thing when you make your height 0 you can't tap it anymore because the label height is zero then how you can tap something has zero height if you make it 20 like the example i provided the label will be clickable if you want to hide the label completely maybe you need to take another approach than tapping the label

答案2

得分: -1

I just sorted the issue.
The issue was I was declaring
var heightCon: NSLayoutConstraint!
as local.

I just declare the same in Global.

Following is the code sorted the issue.

var heightCon: NSLayoutConstraint!
@objc func tapLabel(gesture: UITapGestureRecognizer) {
    isExpanded.toggle()
    if let constraint = heightCon {
        constraint.isActive = false
    }
    heightCon = reminderBulletsLbl.heightAnchor.constraint(equalToConstant: isExpanded ? 230 : 0)
    heightCon.isActive = true
}

Thanks @Sh_Khan for the help.

英文:

I just sorted the issue.
The issue was I was declaring
var heightCon:NSLayoutConstraint!
as local.

I just declare the same in Global.

Following is the code sorted the issue.

var heightCon:NSLayoutConstraint!
    @objc func tapLabel(gesture: UITapGestureRecognizer) {
        isExpanded.toggle()
        if let constraint = heightCon{
            constraint.isActive = false
        }
        heightCon = reminderBulletsLbl.heightAnchor.constraint(equalToConstant: isExpanded ? 230 : 0)
        heightCon.isActive = true
    }

Thanks @Sh_Khan for the help.

huangapple
  • 本文由 发表于 2023年3月4日 00:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定