我有一个定制的UITableViewCell。
以下是再现问题的完整代码。它是在C# (Xamarin),但它是容易理解的想法。这是几乎与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)
});
}
}在发射时,一切都很好:

但单击Update按钮后(重置子视图和约束):
在iOS 7上,没有任何更改(如预期的那样工作)。
在iOS 8上,布局不像预期的那样工作。它似乎失去了一些约束,定义了某些子视图和单元格的内容视图边框之间的边距:

知道为什么会这样吗?它看起来像是iOS自动布局系统中的一个bug。
发布于 2014-10-15 18:41:49
是的,iOS 8 AutoLayout now 确实定义了利润率。
更新
通过这样做可以打破contentView约束:
ContentView.RemoveConstraints(ContentView.Constraints);这一行中断了与其父的contentView关系。移开这条线。
https://stackoverflow.com/questions/26385835
复制相似问题