Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何优化用VFL autolayout编写的代码_Ios_Objective C_Autolayout_Visual Format Language - Fatal编程技术网

Ios 如何优化用VFL autolayout编写的代码

Ios 如何优化用VFL autolayout编写的代码,ios,objective-c,autolayout,visual-format-language,Ios,Objective C,Autolayout,Visual Format Language,我有一个ui视图,我在其中创建ui标签,而不使用循环,并使用VFL。 它工作得很好,但代码可读性很差。即使我也不知道如何使用for循环来创建这些标签。 我必须将高度设置为30,这是我不想要的。我希望它自动增长。 此外,我正在将subcentviewheight常量设置为硬编码constantTheightsubcentview.constant=250 我不想使用视图。 这是我尝试过的代码: -(void)createProfileView{ if (ProfileView == nil) {

我有一个
ui视图
,我在其中创建
ui标签
,而不使用
循环
,并使用
VFL
。 它工作得很好,但代码可读性很差。即使我也不知道如何使用
for
循环来创建这些标签。 我必须将高度设置为
30
,这是我不想要的。我希望它自动增长
。
此外,我正在将
subcentview
height
常量设置为硬编码
constantTheightsubcentview.constant=250
我不想使用视图。
这是我尝试过的代码:

-(void)createProfileView{

if (ProfileView == nil) {
    ProfileView = [[UIView alloc] init];
    [ProfileView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [ProfileView setBackgroundColor:[UIColor yellowColor]];
    [subContentView addSubview:ProfileView];
    views[@"ProfileView"] = ProfileView;

    NSArray* constraints;
    NSString* format;

    format = @"|[ProfileView]|";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [subContentView addConstraints:constraints];

    format = @"V:|-50-[ProfileView]|";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [subContentView addConstraints:constraints];

    //create labels
    //homeTown
    UILabel *lblHomeTown = [self getLabel];
    [lblHomeTown setBackgroundColor:[UIColor greenColor]];
    [lblHomeTown setText:@"Hometown:"];
    [ProfileView addSubview:lblHomeTown];
    views[@"lblHomeTown"] = lblHomeTown;

    UILabel *lblHomeTownDetail = [self getLabel];
    [lblHomeTownDetail setBackgroundColor:[UIColor grayColor]];
    [lblHomeTownDetail setText:self.player.homeTown];
    [ProfileView addSubview:lblHomeTownDetail];
    views[@"lblHomeTownDetail"] = lblHomeTownDetail;

    format = @"|-10-[lblHomeTown]-2-[lblHomeTownDetail]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblHomeTown(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:|[lblHomeTownDetail(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];
    //hometown

    //highSchool
    //create labels
    UILabel *lblHighSchool = [self getLabel];
    [lblHighSchool setBackgroundColor:[UIColor magentaColor]];
    [lblHighSchool setText:@"High School:"];
    [ProfileView addSubview:lblHighSchool];
    views[@"lblHighSchool"] = lblHighSchool;

    UILabel *lblHighSchoolDetail = [self getLabel];
    [lblHighSchoolDetail setBackgroundColor:[UIColor redColor]];
    [lblHighSchoolDetail setText:self.player.highSchool];
    [ProfileView addSubview:lblHighSchoolDetail];
    views[@"lblHighSchoolDetail"] = lblHighSchoolDetail;

    format = @"|-10-[lblHighSchool]-2-[lblHighSchoolDetail]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:|[lblHomeTown(30)]-10-[lblHighSchool(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:|[lblHomeTown(30)]-10-[lblHighSchoolDetail(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];
    //highschool


    //experience
    UILabel *lblExperience = [self getLabel];
    [lblExperience setText:@"Experience:"];
    [ProfileView addSubview:lblExperience];
    views[@"lblExperience"] = lblExperience;

    UILabel *lblExperienceDetail = [self getLabel];
    [lblExperienceDetail setText:self.player.experience];
    [ProfileView addSubview:lblExperienceDetail];
    views[@"lblExperienceDetail"] = lblExperienceDetail;

    format = @"|-10-[lblExperience]-2-[lblExperienceDetail]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblHighSchool(30)]-10-[lblExperience(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblHighSchool(30)]-10-[lblExperienceDetail(30)]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];
     //experience

    //description
    UILabel *lblDescription = [self getLabel];
    [lblDescription setText:self.player.playerDescription];
    [ProfileView addSubview:lblDescription];
    views[@"lblDescription"] = lblDescription;

    format = @"|-10-[lblDescription]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    format = @"V:[lblExperience(30)]-10-[lblDescription]";
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:views];
    [ProfileView addConstraints:constraints];

    //description
}
constantHeightSubContentView.constant = 250;
[subContentView bringSubviewToFront:ProfileView];
}


}

令人惊讶的答案,唯一的问题是您必须使用VFL来指定乘数值,而不是如答案中所示的界面生成器。但是,一旦您理解了所使用的逻辑,这应该很容易。

令人惊讶的答案,唯一的问题是您必须使用VFL来指定乘数值,而不是如答案中所示的界面生成器。但是,一旦您理解了所使用的逻辑,这应该很容易。

您考虑过将UIView子类化吗?这将清理您的代码,并允许您更好地构建代码。是的,这是我目前正在做的第一件事。我还创建了一个行视图(UIView子类,它将有两个水平分隔的标签)。然后我将使用for循环垂直绘制3/n行(行视图)。我的问题仍然是,如何使用for循环和VFL或最后优先级的“constraintWithItem”方法垂直创建三个或n个视图?您考虑过将UIView子类化吗?这将清理您的代码,并允许您更好地构建代码。是的,这是我目前正在做的第一件事。我还创建了一个行视图(UIView子类,它将有两个水平分隔的标签)。然后我将使用for循环垂直绘制3/n行(行视图)。我的问题仍然是,如何使用for循环和VFL或最后优先级的“constraintWithItem”方式垂直创建三个或n个视图
-(UILabel *)getLabel{

UILabel *lbl = [[UILabel alloc] init];
[lbl setTranslatesAutoresizingMaskIntoConstraints:NO];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setNumberOfLines:0];
[lbl setTextColor:[UIColor blackColor]];
[lbl setFont:[UIFont fontWithName:@"AppleSDGothicNeo-Regular" size:13.0f]];
lbl.lineBreakMode = NSLineBreakByWordWrapping;
return lbl;