在使用自动布局时,在iOS 8上旋转单元格高度会丢失 [英] On rotation cell height gets lost on iOS 8 when using auto layout
问题描述
通常,iOS 8应该能够计算单元格本身的高度。到目前为止这是有效的,但是如果我旋转设备,则单元格会恢复到44点的标准高度。
这是它的样子:
这就是它的作用轮换后看起来像:
一旦旋转,它就会停留在那个布局中。它再也不会计算出真正的高度了。我不知道为什么。我添加了完整的代码。对于约束,请查看 updateConstraints
。代码在C#中,但您应该能够阅读它。您可以在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;
}
我的数据来源:
public class TestSource:UITableViewSource
{
public static readonly NSString cellIdentifier = new NSString(CustomCell);
私有TestTableVC控制器;
public TestSource(TestTableVC controller)
{
this.controller = controller;
}
公共覆盖int RowsInSection(UITableView tableview,int section)
{
return 10;
}
公共覆盖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();
返回单元格;
}
}
我的 CustomCell
:
公共类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 =性别+:+性别;
birthdateLabel.Text =生于+:+ 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;
//在iOS 7(自动调整掩码)和iOS 8(layoutSubViews)中调整内容视图的大小。
//不知道为什么会发生这种情况,但它应该只涉及nibs,如果你使用iOS 8 SDK并且为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)){
//只需要其中一个语句
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);
}
}
有什么不对我的约束或是另一个iOS 8错误? BTW:我正在使用Xcode 6.1和iOS 8.1 SDK,并希望支持iOS 7作为iOS 8设备。 iOS 7是另一个故事。
在 viewWillTransitionToSize
函数中,设置 self.tableView.estimatedRowHeight =您估计的行高
,它似乎能够解决问题,也许这是苹果的一个错误,可以在将来进行改进。 / p>
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.
In viewWillTransitionToSize
function, set self.tableView.estimatedRowHeight = your estimated row height
, it seems to be able to fix the problem, and perhaps it is a bug from apple which can be improved in the future.
这篇关于在使用自动布局时,在iOS 8上旋转单元格高度会丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!