Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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 为什么我的自动布局标签没有按预期换行?_Ios_Objective C_Autolayout - Fatal编程技术网

Ios 为什么我的自动布局标签没有按预期换行?

Ios 为什么我的自动布局标签没有按预期换行?,ios,objective-c,autolayout,Ios,Objective C,Autolayout,首先,我创建我的新标签: UILabel *newLabel=[[UILabel alloc] init]; newLabel.font=[UIFont preferredFontForTextStyle:@"Body"]; newLabel.translatesAutoresizingMaskIntoConstraints=NO; newLabel.numberOfLines=0; newLabel.lineBreakMode=NSLineBreakByWordWrapping; newLab

首先,我创建我的新标签:

UILabel *newLabel=[[UILabel alloc] init];
newLabel.font=[UIFont preferredFontForTextStyle:@"Body"];
newLabel.translatesAutoresizingMaskIntoConstraints=NO;
newLabel.numberOfLines=0;
newLabel.lineBreakMode=NSLineBreakByWordWrapping;
newLabel.text=[NSString stringWithFormat:@"This line is this: %@%@%@",resultString,resultString,resultString];
然后,我创建各种约束:

NSArray *labelConstraints=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]"
                                                                      options:0
                                                                      metrics:nil
                                                                      views:@{@"label": newLabel}];
//Merge the above constraint into a tracking array
labelConstraints=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|"
                                                             options:0
                                                             metrics:nil
                                                               views:@{@"label": newLabel}];
//Again, move the constraint into a tracking array

//Later, we apply all constraints in the tracking array to self.
不幸的是,标签的行为与预期不符。假设我正确阅读了上面的约束,我的标签应该水平地从包含视图的一个边缘移动到另一个边缘,而不是垂直地绑定到任何给定的高度。这应该与我的设置numberOfLines=0和lineBreakMode=NSLineBreakByWordWrapping相结合,导致单元格水平拉伸以获取所有必要的行。相反,这条线将自己延伸到包含视图的边缘之外,以便将所有内容都放在一条线上——我不知道为什么


如果有关系的话,[self]是UITableViewCell的一个子类,我正试图对其进行编程,以根据内容大小动态扩展。如果我能让单元格的内容正确布局,那么计算单元格的实际大小应该相对容易

您可能需要设置
preferredMaxLayoutWidth

见类似问题:


不幸的是,将
UILabel
numberOfLines
设置为零并不能很好地使用“开箱即用”的自动布局。您还需要设置标签的属性。

是否尝试为约束提供宽度值?比如:@“H:[label(当我遇到这个问题并找到解决方案时,我在这里使用了UILabel子类solution: