When I add a subview to a prototype table cell, let's say an UITextView
, then there is no left margin by default.
I would like it to use the same margin that iOS uses internally for built-in standard cell layouts.
In case I'm missing the obvious, where is the "use device/iOS defaults" flag? Otherwise, my preferred solution would be a way to query the device for its device-dependent and iOS version dependent UI metrics. For the purpose of this question, we can limit this to only the metrics of the UITableView
control and its UITableCell
descendants.
This is how I generate my custom cell:
internal class UITextSingleline : UITableViewCell
{
UILabel headingLabel;
UITextView textBox;
int MultiHeight;
public bool secure;
public UITextSingleline() : this(null, 1) { }
public UITextSingleline(int multiheight) : this(null, multiheight) { }
public UITextSingleline(NSString cellId, int multiheight) : base(UITableViewCellStyle.Default, cellId)
{
MultiHeight = multiheight;
SelectionStyle = UITableViewCellSelectionStyle.None;
headingLabel = new UILabel()
{
Font = UIFont.SystemFontOfSize(16),
TextColor = UIColor.DarkTextColor,
BackgroundColor = UIColor.Clear
};
textBox = new UITextView()
{
ClipsToBounds = true,
Font = UIFont.SystemFontOfSize(16),
TextColor = UIColor.DarkTextColor
};
if (multiheight == 1) textBox.TextContainer.MaximumNumberOfLines = 1;
textBox.Layer.CornerRadius = 10.0f;
textBox.Layer.BorderColor = UIColor.DarkTextColor.CGColor;
textBox.Layer.BorderWidth = 1f;
ContentView.AddSubviews(new UIView[] { headingLabel, textBox });
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
headingLabel.Frame = new CGRect(16, 8, ContentView.Bounds.Width - 32, 20);
textBox.Frame = new CGRect(16, 32, ContentView.Bounds.Width - 32, 36 * MultiHeight); /* see? magic numbers all over the place */
}
public void UpdateCell(string caption, string text)
{
headingLabel.Text = caption;
textBox.Text = text;
}
public string Text
{
get
{
return textBox?.Text;
}
set
{
if (textBox != null) textBox.Text = value;
}
}
}
And this is how it is linked to the containing table view:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
switch (indexPath.Section)
{
case 0:
/* area/workplace */
switch (indexPath.Row)
{
case 0:
/* area picker (omitted) */
case 1:
/* workplace text input (single-line) */
if (txtWorkplace == null)
{
txtWorkplace = new UITextSingleline();
txtWorkplace.UpdateCell("Workplace", Data.Instance.Payload.report.workplace);
}
return txtWorkplace;
}
break;
case 1:
/* rest ommitted for brevity */
break;
}
return null;
}
I have searched SO and the internet for an equivalent to systemmetrics, there is lots for colors and fonts but I found only little information about dimensions, margins, insets, corner radii, and the like:
- This question deals with the opposite, getting rid of any margin, and does not mention how to replicate the iOS defaults.
- This page from apple dev mentions the
separatorInset
property of the wholeTableView
, which seems to work for the left indentation, but I'm somewhat sceptical about applying a metric for one part of the layout to another part.
Hardcoded magic numbers are not an option. We tested on iPhone and iPad and noticed varying inset defaults even on the same device with only the iOS version different. I'd be happy with objective-C and Swift hints and solutions too, as long as they work in xamarin once properly translated.