Normally iOS 8 should be able to calculate the height of a cell itself. This is working so far, but if I rotate the device the cell gets back to its standard height of 44 points.
This is how it should look like:
This is how it does look like after rotation:
Once rotated it stays in that layout. It never calculates the real height anymore. I don't know why. I have added my complete code. For the constraints have a look into updateConstraints
. The code is in C# but you should be able to read it. You can post your solution of course in Objective-C.
VC viewDidLoad
:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.TableView.Source = new TestSource (this);
this.TableView.RegisterClassForCellReuse (typeof(CustomCell), TestSource.cellIdentifier);
//this.TableView.RowHeight = 83;
}
My data source:
public class TestSource : UITableViewSource
{
public static readonly NSString cellIdentifier = new NSString("CustomCell");
private TestTableVC controller;
public TestSource (TestTableVC controller)
{
this.controller = controller;
}
public override int RowsInSection (UITableView tableview, int section)
{
return 10;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
CustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as CustomCell;
cell.UpdateCell ("Max Mustermann", "m", new DateTime (2014, 12, 10), "1234567890");
cell.SetNeedsUpdateConstraints ();
cell.UpdateConstraintsIfNeeded ();
return cell;
}
}
My CustomCell
:
public class CustomCell : UITableViewCell
{
public static readonly NSString cellIdentifier = new NSString("CustomCell");
private UILabel fullNameLabel, genderLabel, birthdateLabel, iNumberLabel;
private bool didSetupConstraints = false;
public CustomCell ()
{
this.CreateView ();
}
public CustomCell (IntPtr handle) : base(handle)
{
this.CreateView ();
}
public void UpdateCell (string fullName, string gender, DateTime? birthdate, string iNumber)
{
fullNameLabel.Text = fullName;
genderLabel.Text = "Gender" + ": " + gender;
birthdateLabel.Text = "Born on" + ": " + dateOfBirth.ToString ("dd.MM.yyyy");
iNumberLabel.Text = iNumber;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
this.ContentView.SetNeedsLayout();
this.ContentView.LayoutIfNeeded();
}
public override void UpdateConstraints()
{
if (!didSetupConstraints) {
NSMutableDictionary viewsDictionary = new NSMutableDictionary ();
viewsDictionary ["fullNameLabel"] = fullNameLabel;
viewsDictionary ["genderLabel"] = genderLabel;
viewsDictionary ["birthdateLabel"] = birthdateLabel;
viewsDictionary ["iNumberLabel"] = iNumberLabel;
fullNameLabel.TranslatesAutoresizingMaskIntoConstraints = false;
genderLabel.TranslatesAutoresizingMaskIntoConstraints = false;
birthdateLabel.TranslatesAutoresizingMaskIntoConstraints = false;
iNumberLabel.TranslatesAutoresizingMaskIntoConstraints = false;
// Sizing of content view is differently in iOS 7 (autoresizing mask) and iOS 8 (layoutSubViews).
// Don't know why this occurs but it should only concern nibs and if you are using iOS 8 SDK and compile for iOS 7.
// http://stackoverflow.com/questions/24750158/autoresizing-issue-of-uicollectionviewcell-contentviews-frame-in-storyboard-pro
// http://stackoverflow.com/questions/19132908/auto-layout-constraints-issue-on-ios7-in-uitableviewcell
// bug?
if (!UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
// only one of these statements is needed
this.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
this.ContentView.Bounds = new RectangleF (0, 0, 99999, 99999);
}
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[fullNameLabel]", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[genderLabel]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[birthdateLabel]", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("V:|-(8)-[fullNameLabel]-(5)-[genderLabel]-(5)-[birthdateLabel]-(8)-|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraint (NSLayoutConstraint.Create (iNumberLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.CenterY, 1, 0));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:[iNumberLabel]-|", (NSLayoutFormatOptions)0, null, viewsDictionary));
didSetupConstraints = true;
}
base.UpdateConstraints ();
}
private void CreateView()
{
fullNameLabel = new UILabel () {
Font = UIFont.BoldSystemFontOfSize(17),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
genderLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
birthdateLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
iNumberLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Right
};
ContentView.AddSubviews (fullNameLabel, genderLabel, birthdateLabel, iNumberLabel);
}
}
Is something wrong with my constraints or is this another iOS 8 bug? BTW: I'm using Xcode 6.1 with the iOS 8.1 SDK and want to support iOS 7 as iOS 8 devices. iOS 7 is another story.