繁体   English   中英

ios autolayout类似

[英]ios autolayout similar

我对ios很新。 在编写我的第一个应用程序时,我遇到了自动布局仅适用于ios 6.0。 我希望我的应用程序至少5.0 ios。

也许有人会知道如何在没有autolayout的情况下做到这一点。

我有标签,其中包含动态文本,1行或2行或3行取决于用户设置。 在它下面我有uitextfield。 使用自动布局我没有头痛,因为它完成了所有工作,文本字段位于下面的1 2或3行文本中(调整大小,自动移动)。

那么如何在没有autolayout的情况下做到这一点呢?

如果没有自动布局,您必须在代码中处理此问题。 建议的方法是对容器视图(包含标签和文本字段的视图)进行子类化,并覆盖layoutSubviews方法。 在那里,您可以根据所需的条件(例如, 标签文本指标 )手动设置视图子视图的框架。

编辑 :这是一个特定的例子,可以在containverView layoutSubviews方法(从我的头顶输入):

// Those could be IBOutlets, or obtained by inspecting self.subviews
UILabel *label = self.label;
UITextField *textField = self.textField;

// Determine the labelSize, we could limit the maxSize (especially the height) manually here
CGSize maxSize = self.bounds.size;
CGSize labelSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];

// Set the computed label size
CGRect labelFrame = label.frame;
labelFrame.size = labelSize;
label.frame = labelFrame;

// Now move the textField just below the label (we could also add a vertical margin here if we want)
CGRect textFieldFrame = textField.frame;
textFieldFrame.origin.y = labelFrame.origin.y + labelFrame.size.height;
textField.frame = textFieldFrame;

这只是确保文本字段始终位于标签下方。 根据您所使用的限制,您可能需要添加更多代码以确保UI正确布局。

您还需要确保在标签文本更改时调用[containverView setNeedsLayout]

如果您使用的是nib或storybord,则只需删除文件检查器中的标记即可 在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM