首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >奇怪的iOS 8自动布局问题

奇怪的iOS 8自动布局问题
EN

Stack Overflow用户
提问于 2014-10-15 15:04:03
回答 1查看 833关注 0票数 1

我有一个定制的UITableViewCell。

  1. 最初,在单元格的内容视图上设置了一些子视图和一些自动布局约束。一切都如期而至。
  2. 然后以编程方式更改(删除/添加)一些子视图和完全重置约束(删除所有约束,然后添加新约束)。然后,刷新包含单元格的UITable (重新计算行的高度),并在单元格上显式调用setNeedsUpdateContraints、updateConstraintsIfNeeded。

以下是再现问题的完整代码。它是在C# (Xamarin),但它是容易理解的想法。这是几乎与Swift中的代码相同。

代码语言:javascript
运行
复制
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)
            });
    }
}

在发射时,一切都很好:

但单击Update按钮后(重置子视图和约束):

iOS 7上,没有任何更改(如预期的那样工作)。

iOS 8上,布局不像预期的那样工作。它似乎失去了一些约束,定义了某些子视图和单元格的内容视图边框之间的边距:

知道为什么会这样吗?它看起来像是iOS自动布局系统中的一个bug。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-15 18:41:49

是的,iOS 8 AutoLayout now 确实定义了利润率。

更新

通过这样做可以打破contentView约束:

代码语言:javascript
运行
复制
ContentView.RemoveConstraints(ContentView.Constraints);

这一行中断了与其父的contentView关系。移开这条线。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26385835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档