[英]On rotation cell height gets lost on iOS 8 when using auto layout
Normally iOS 8 should be able to calculate the height of a cell itself. 通常,iOS 8应该能够计算单元格本身的高度。 This is working so far, but if I rotate the device the cell gets back to its standard height of 44 points. 到目前为止,这种方法一直有效,但是如果我旋转设备,则像元将返回到其标准高度44点。
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
. 对于约束,请查看updateConstraints
。 The code is in C# but you should be able to read it. 该代码在C#中,但是您应该可以阅读它。 You can post your solution of course in Objective-C. 您当然可以在Objective-C中发布您的解决方案。
VC viewDidLoad
: 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
: 我的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? 我的约束有问题吗,还是这是另一个iOS 8错误? BTW: I'm using Xcode 6.1 with the iOS 8.1 SDK and want to support iOS 7 as iOS 8 devices. 顺便说一句:我正在将Xcode 6.1与iOS 8.1 SDK一起使用,并希望将iOS 7作为iOS 8设备支持。 iOS 7 is another story. iOS 7是另一个故事。
在viewWillTransitionToSize
函数中,设置self.tableView.estimatedRowHeight = your estimated row height
,似乎可以解决此问题,也许这是苹果的一个错误,将来可以改进。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.