• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 我们如何使用自动布局在 for 循环的帮助下创建 UIlabels?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:43:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的项目中,我使用了自动布局,并在我的 View Controller 上添加了一个或多个标签,为此我想通过代码减少的目的使用“for”循环,但使用“constraintWithVisualFormate”。

我不明白!

如何使用 for 循环?

我的代码:

emailTextField = [[UILabel alloc] init];
emailTextField.text = @"MD (Medician)";
emailTextField.textColor = [UIColor blackColor];
emailTextField.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview: emailTextField];

nameTextField = [[UILabel alloc] init];
nameTextField.text = @"Experience:12 Years";
nameTextField.textColor = [UIColor blackColor];
nameTextField.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview: nameTextField];

password = [[UILabel alloc] init];
password.text = @"Experience:12 Years";
password.textColor = [UIColor blackColor];
password.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview: password];


//Applying auto-layouts for labels

NSDictionary * views = NSDictionaryOfVariableBindings(emailTextField,nameTextField,password);

NSArray *textFields = @[emailTextField, nameTextField, password];

for (UITextField *textField in textFields) {

   [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"H:|-10-[textField]-10-|"
                                                                     options:0
                                                                     metrics:nil
                                                                       views:viewsDic]];
}

我在上面的 for 循环附近苦苦挣扎,因为我不明白如何在 for 循环中插入 nameTextFieldemailTextField...等标签。



Best Answer-推荐答案


截图

代码

 emailTextField = [self createLabelWithText: @"MD (Medician)"];
[self.view addSubview: emailTextField];

nameTextField = [self createLabelWithText: @"Experience:12 Years"];
[self.view addSubview: nameTextField];

passwword = [self createLabelWithText: @"Experience:12 Years"];
[self.view addSubview: passwword];

NSDictionary * viewsDic = NSDictionaryOfVariableBindings(emailTextField,nameTextField,passwword);

NSArray * keys = @[@"emailTextField",@"nameTextField",@"passwword"];

for (NSString * key in keys) {
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat"H:|-10-[%@]-10-|",key]
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewsDic]];
}
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat"V:|-20-[emailTextField]-[nameTextField]-[passwword]"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDic]];

这是函数

-(UILabel *)createLabelWithTextNSString *)text{
    UILabel * label = [[UILabel alloc] init];
    label.text = text;
    label.textColor = [UIColor blackColor];
    label.translatesAutoresizingMaskIntoConstraints = NO;
    return label;
}

关于ios - 我们如何使用自动布局在 for 循环的帮助下创建 UIlabels?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206481/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap