简体   繁体   English

ios autolayout类似

[英]ios autolayout similar

I'm pretty new to ios. 我对ios很新。 And while writing my first app i'v encountered that autolayout is only for ios 6.0. 在编写我的第一个应用程序时,我遇到了自动布局仅适用于ios 6.0。 And i'd wish to make my app for at least 5.0 ios. 我希望我的应用程序至少5.0 ios。

Maybe anyone would know how to make this without autolayout. 也许有人会知道如何在没有autolayout的情况下做到这一点。

I have label which has dynamic text, 1row or 2 rows or 3 rows depends on user settings. 我有标签,其中包含动态文本,1行或2行或3行取决于用户设置。 And below it i have uitextfield. 在它下面我有uitextfield。 With autolayout i have no headache as it does all the work, the textfield sits nicely below in 1 2 or 3 rows of text above (resizes, moves automatically). 使用自动布局我没有头痛,因为它完成了所有工作,文本字段位于下面的1 2或3行文本中(调整大小,自动移动)。

So how should i do this without autolayout? 那么如何在没有autolayout的情况下做到这一点呢?

Without autolayout you have to handle this in code. 如果没有自动布局,您必须在代码中处理此问题。 The recommended way of doing this would be to subclass your container view (the view that contains your label and text filed) and override the layoutSubviews method. 建议的方法是对容器视图(包含标签和文本字段的视图)进行子类化,并覆盖layoutSubviews方法。 In there you set the frames of the view's subviews manually, based on your desired criteria (eg, the label text metrics ). 在那里,您可以根据所需的条件(例如, 标签文本指标 )手动设置视图子视图的框架。

EDIT : here's a specific example of something that could be in the containverView layoutSubviews method (typed from the top of my head): 编辑 :这是一个特定的例子,可以在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;

This just makes sure the text field is always below the label. 这只是确保文本字段始终位于标签下方。 Depending on the constraints you had in place, you might need to add more code to make sure the UI lays out correctly. 根据您所使用的限制,您可能需要添加更多代码以确保UI正确布局。

You also need to make sure that [containverView setNeedsLayout] gets called when the label text changes. 您还需要确保在标签文本更改时调用[containverView setNeedsLayout]

if you are using nib or storybord you can just remove the mark in the file inspector 如果您使用的是nib或storybord,则只需删除文件检查器中的标记即可 在此输入图像描述

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

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