ios - 我们如何使用自动布局在 for 循环的帮助下创建 UIlabels?
<p><p>在我的项目中,我使用了自动布局,并在我的 ViewController 上添加了一个或多个标签,为此我想通过代码减少的目的使用“for”循环,但使用“constraintWithVisualFormate”。 </p>
<p>我不明白!</p>
<p>如何使用 for 循环?</p>
<h2>我的代码:</h2>
<pre><code>emailTextField = [ init];
emailTextField.text = @"MD (Medician)";
emailTextField.textColor = ;
emailTextField.translatesAutoresizingMaskIntoConstraints = NO;
;
nameTextField = [ init];
nameTextField.text = @"Experience:12 Years";
nameTextField.textColor = ;
nameTextField.translatesAutoresizingMaskIntoConstraints = NO;
;
password = [ init];
password.text = @"Experience:12 Years";
password.textColor = ;
password.translatesAutoresizingMaskIntoConstraints = NO;
;
//Applying auto-layouts for labels
NSDictionary * views = NSDictionaryOfVariableBindings(emailTextField,nameTextField,password);
NSArray *textFields = @;
for (UITextField *textField in textFields) {
-10-|"
options:0
metrics:nil
views:viewsDic]];
}
</code></pre>
<p>我在上面的 for 循环附近苦苦挣扎,因为我不明白如何在 for 循环中插入 <code>nameTextField</code>、<code>emailTextField</code>...等标签。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>截图</p>
<p> <img src="/image/1i4rS.png" width="320"/> </p>
<p>代码</p>
<pre><code> emailTextField = ;
;
nameTextField = ;
;
passwword = ;
;
NSDictionary * viewsDic = NSDictionaryOfVariableBindings(emailTextField,nameTextField,passwword);
NSArray * keys = @[@"emailTextField",@"nameTextField",@"passwword"];
for (NSString * key in keys) {
-10-|",key]
options:0
metrics:nil
views:viewsDic]];
}
--"
options:0
metrics:nil
views:viewsDic]];
</code></pre>
<p>这是函数</p>
<pre><code>-(UILabel *)createLabelWithText:(NSString *)text{
UILabel * label = [ init];
label.text = text;
label.textColor = ;
label.translatesAutoresizingMaskIntoConstraints = NO;
return label;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 我们如何使用自动布局在 for 循环的帮助下创建 UIlabels?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33206481/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33206481/
</a>
</p>
页:
[1]