如果要根据不同设备或不同版本使用默认边距,为什么不尝试自动布局?
NSLayoutAttribute.LeadingMargin
表示元素边距的默认前缘。在你的
UITextSingleline
修改
LayoutSubviews()
从硬代码到自动布局:
假设单元格只有一个标签来显示某些文本:
public override void LayoutSubviews()
{
base.LayoutSubviews();
var leadingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.LeadingMargin, 1.0f, 0);
var topConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TopMargin, 1.0f, 0);
var trailingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TrailingMargin, 1.0f, 0);
var bottomConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.BottomMargin, 1.0f, 0);
ContentView.AddConstraints(new NSLayoutConstraint[] { leadingConstraint, topConstraint, trailingConstraint, bottomConstraint });
}
这样的话,
headingLabel
将具有与“标准内置单元格的文本标签”相同的布局。
此外,在您的情况下,似乎您要添加
UITextView
也在你的牢房里。我建议您在构造函数时添加约束,我提供我的约束供您参考:
public MyTableViewCell (IntPtr handle) : base (handle)
{
headingLabel = new UILabel()
{
Font = UIFont.SystemFontOfSize(17),
TextColor = UIColor.DarkTextColor,
BackgroundColor = UIColor.Clear,
Lines = 0
};
textBox = new UITextView()
{
ClipsToBounds = true,
Font = UIFont.SystemFontOfSize(16),
TextColor = UIColor.DarkTextColor
};
ContentView.AddSubview(headingLabel);
ContentView.AddSubview(textBox);
// Disable this to enable autolayout
headingLabel.TranslatesAutoresizingMaskIntoConstraints = false;
textBox.TranslatesAutoresizingMaskIntoConstraints = false;
doLayouts();
}
void doLayouts()
{
var leadingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.LeadingMargin, 1.0f, 0);
var topConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TopMargin, 1.0f, 0);
var trailingConstraint = NSLayoutConstraint.Create(headingLabel, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TrailingMargin, 1.0f, 0);
ContentView.AddConstraints(new NSLayoutConstraint[] { leadingConstraint, topConstraint, trailingConstraint });
var boxLeading = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.LeadingMargin, 1.0f, 0);
var boxTop = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Top, NSLayoutRelation.Equal, headingLabel, NSLayoutAttribute.Bottom, 1.0f, 4);
var boxTrailing = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.TrailingMargin, 1.0f, 0);
var boxBottom = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.BottomMargin, 1.0f, 0);
var boxHeight = NSLayoutConstraint.Create(textBox, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, 36 * MultiHeight);
ContentView.AddConstraints(new NSLayoutConstraint[] { boxLeading, boxTop, boxTrailing, boxBottom, boxHeight });
}
使用autolayout的另一个好处是:将tableview的rowheight设置为
UITableView.AutomaticDimension
如果我们设置了正确的约束,单元格将根据内容自动计算行高。