View height constraints are not set correctly as I scroll UICollectionView up and down

前端 未结 2 1301
青春惊慌失措
青春惊慌失措 2021-01-23 19:14

I\'m trying to decide view heights based on a model property, but as UICollectionView is scrolled up and down, incorrect heights are assigned to visible cells. It s

相关标签:
2条回答
  • 2021-01-23 19:53

    If you do this, the print message should prompt you to repeat the constraint.

    You set the constraints of left, right, bottom, top, and added a height constraint when updating. The first four constraints have already determined the final height, the new height here will not work, and a warning message will be printed.

    If you really want to update the height, you should set the left, right, top, height constraints from the beginning, and save the height constraint, which is used when updating.

    var heightConstraint: NSLayoutConstraint?
    
    heightConstraint = _uiView.heightAnchor.constraint(equalToConstant: 50)//Defaults
    NSLayoutConstraint.activate([
    
    (_uiView.topAnchor.constraint(equalTo:ContentView.SafeAreaLayoutGuide.topAnchor))!,                
    (_uiView.leftAnchor.constraint(equalTo:ContentView.SafeAreaLayoutGuide.leftAnchor))!
    (_uiView.rightAnchor.constraint(equalTo:ContentView.SafeAreaLayoutGuide.rightAnchor))!,
    (heightConstraint)!
    ]);
    
    
    public void UpdateHeight(int height){
        heightConstraint?.isActive = false
        heightConstraint = _uiView.heightAnchor.constraint(equalToConstant: height)
        heightConstraint?.isActive = true
    }
    
    0 讨论(0)
  • 2021-01-23 20:09

    Here's the fix for this as recommended by Xamarin support:

    NSLayoutConstraint heightConstraint;
    public void UpdateHeight(int height)
    {
        if (heightConstraint == null)
        {
            heightConstraint = _uiView.HeightAnchor.ConstraintEqualTo(height);
            heightConstraint.Active = true;
        }
        else
        {
            heightConstraint.Constant = height;
        }
    }
    
    0 讨论(0)
提交回复
热议问题