gpt4 book ai didi

ios - 当我上下滚动 UICollectionView 时, View 高度约束设置不正确

转载 作者:行者123 更新时间:2023-11-29 05:22:10 25 4
gpt4 key购买 nike

我尝试根据模型属性决定 View 高度,但当 UICollectionView 上下滚动时,会为可见单元格分配不正确的高度。似乎在 GetCell (即 cellForItemAtIndexPath)中设置 HeightAnchor 不起作用。我怎样才能做到这一点?

using CoreGraphics;
using Foundation;
using System;
using System.Collections.Generic;
using UIKit;

namespace App2
{
public partial class ViewController : UIViewController
{
private UICollectionView _collectionView;

public ViewController (IntPtr handle) : base (handle)
{
}

public override void ViewDidLoad ()
{
base.ViewDidLoad ();

InitializeCollectionView();
}

private void InitializeCollectionView()
{
_collectionView = new UICollectionView(View.Frame, new UICollectionViewCompositionalLayout(GetSection()))
{
DataSource = new CustomUICollectionViewDataSource(),
TranslatesAutoresizingMaskIntoConstraints = false
};

_collectionView.RegisterClassForCell(typeof(CustomUICollectionViewCell), "CustomUICollectionViewCell");

View.AddSubview(_collectionView);

NSLayoutConstraint.ActivateConstraints(new[]
{
_collectionView.TopAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.TopAnchor),
_collectionView.BottomAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.BottomAnchor),
_collectionView.LeftAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.LeftAnchor),
_collectionView.RightAnchor.ConstraintEqualTo(View.SafeAreaLayoutGuide.RightAnchor)
});
}

private static NSCollectionLayoutSection GetSection()
{
var size = NSCollectionLayoutSize.Create(NSCollectionLayoutDimension.CreateFractionalWidth(1), NSCollectionLayoutDimension.CreateEstimated(50));
var item = NSCollectionLayoutItem.Create(size);
var group = NSCollectionLayoutGroup.CreateHorizontal(layoutSize: size, subitem: item, count: 1);
var section = NSCollectionLayoutSection.Create(group);

section.InterGroupSpacing = 5;

return section;
}
}

public class CustomUICollectionViewDataSource : UICollectionViewDataSource
{
private readonly List<Model> _models = new List<Model>
{
new Model {Height = 250},
new Model {Height = 100},
new Model {Height = 300},
new Model {Height = 400},
new Model {Height = 500},
new Model {Height = 50},
new Model {Height = 230},
new Model {Height = 100},
new Model {Height = 600},
new Model {Height = 310},
new Model {Height = 150},
new Model {Height = 220}
};

public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var model = _models[(int)indexPath.Item];

var cell = collectionView.DequeueReusableCell("CustomUICollectionViewCell", indexPath) as CustomUICollectionViewCell;

cell.UpdateHeight(model.Height);

return cell;
}

public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return _models.Count;
}
}

public sealed class CustomUICollectionViewCell : UICollectionViewCell
{
private readonly UIView _uiView;

[Export("initWithFrame:")]
public CustomUICollectionViewCell(CGRect frame) : base(frame)
{
_uiView = new UIView
{
BackgroundColor = UIColor.Brown,
TranslatesAutoresizingMaskIntoConstraints = false
};

ContentView.AddSubview(_uiView);

NSLayoutConstraint.ActivateConstraints(new[]
{
_uiView.TopAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.TopAnchor),
_uiView.BottomAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.BottomAnchor),
_uiView.LeftAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.LeftAnchor),
_uiView.RightAnchor.ConstraintEqualTo(ContentView.SafeAreaLayoutGuide.RightAnchor)
});
}

public void UpdateHeight(int height)
{
_uiView.HeightAnchor.ConstraintEqualTo(height).Active = true;
}
}

public class Model
{
public int Height { get; set; }
}
}

最佳答案

如果您这样做,打印消息将提示您重复约束。

您设置了左、右、下、上约束,并在更新时添加了高度约束。前四个约束已经确定了最终的高度,这里的新高度将不起作用,并且会打印一条警告消息。

如果确实要更新高度,则应从一开始就设置left、right、top、height约束,并保存height约束,即更新时使用。

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
}

关于ios - 当我上下滚动 UICollectionView 时, View 高度约束设置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58580490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com