gpt4 book ai didi

ios - 奇怪的 iOS 8 自动布局问题

转载 作者:行者123 更新时间:2023-12-01 18:56:32 25 4
gpt4 key购买 nike

我有一个自定义 UITableViewCell。

  • 最初,在单元格的内容 View 上设置了一些 subview 和一些自动布局约束。一切都按预期工作。
  • 然后我以编程方式更改(删除/添加)一些 subview 并完全重置约束(删除所有,然后添加新约束)。之后,包含单元格的 UITable 会被刷新(重新计算行高)并且 setNeedsUpdateContraints、updateConstraintsIfNeeded 也会在单元格上显式调用。

  • 这是重现该问题的完整代码。它在 C# (Xamarin) 中,但很容易阅读这个想法。 Here is Swift 中几乎相同的代码。
    public class TableViewController : UITableViewController
    {
    public override void ViewDidLoad()
    {
    base.ViewDidLoad();

    var source = new TableSource(TableView);

    var updateButton = new UIBarButtonItem("Update", UIBarButtonItemStyle.Plain, (s, args) => source.DoUpdate());
    NavigationItem.RightBarButtonItem = updateButton;

    TableView.Source = source;
    TableView.ReloadData();
    }
    }

    public class TableSource : UITableViewSource
    {
    private readonly UITableView _tableView;
    private readonly TableCell _cell;

    public TableSource(UITableView tableView)
    {
    _tableView = tableView;
    _cell = new TableCell();
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
    return _cell;
    }

    public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
    {
    return 55.0f;
    }

    public override int RowsInSection(UITableView tableview, int section)
    {
    return 1;
    }

    public void DoUpdate()
    {
    _cell.DoUpdate();
    _tableView.ReloadData();
    }
    }

    public class TableCell : UITableViewCell
    {
    private UILabel _label;

    public TableCell()
    {
    DoUpdate();
    }

    public void DoUpdate()
    {
    foreach (var subview in ContentView.Subviews)
    {
    subview.RemoveFromSuperview();
    }

    _label = new UILabel {Text = "Text here"};
    ContentView.Add(_label);

    foreach (var subview in ContentView.Subviews)
    {
    subview.TranslatesAutoresizingMaskIntoConstraints = false;
    }

    ContentView.RemoveConstraints(ContentView.Constraints);
    ContentView.AddConstraints(
    new[]
    {
    NSLayoutConstraint.Create(_label, NSLayoutAttribute.Left, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Left, 1.0f, 15.0f),
    NSLayoutConstraint.Create(_label, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, 100.0f),
    NSLayoutConstraint.Create(_label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1.0f, 20.0f),
    NSLayoutConstraint.Create(_label, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Bottom, 1.0f, -20.0f)
    });
    }
    }

    启动时一切正常:

    enter image description here

    但点击更新按钮后(重置 subview 和约束):

    iOS 7 没有任何变化(按预期工作)。

    iOS 8 布局没有按预期工作。看起来它失去了一些约束,定义了一些 subview 和单元格的内容 View 边界之间的边距:

    enter image description here

    任何想法为什么会发生这种情况?它看起来像 iOS 自动布局系统中的一个错误。

    最佳答案

    是的,iOS 8 AutoLayout 现在 真的定义边距。

    更新

    你打破了contentView这样做的约束:

    ContentView.RemoveConstraints(ContentView.Constraints);

    此换行符 contentView与其 parent 的关系。删除此行。

    关于ios - 奇怪的 iOS 8 自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385835/

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