Ios 更新大小未知的多行UILabel的preferredMaxLayoutWidth(自动布局)
我有一个自定义视图类,它继承自Ios 更新大小未知的多行UILabel的preferredMaxLayoutWidth(自动布局),ios,ios6,uilabel,autolayout,Ios,Ios6,Uilabel,Autolayout,我有一个自定义视图类,它继承自UIView。此类有一个UILabel作为其子视图。在这个自定义视图类的init-函数中,我设置了如下所需的所有内容: //h-file #import <UIKit/UIKit.h> @interface MyCustomView : UIView @property (strong, nonatomic) UILabel *myLabel; @end //m-file @implementation MyCustomView @synthe
UIView
。此类有一个UILabel
作为其子视图。在这个自定义视图类的init
-函数中,我设置了如下所需的所有内容:
//h-file
#import <UIKit/UIKit.h>
@interface MyCustomView : UIView
@property (strong, nonatomic) UILabel *myLabel;
@end
//m-file
@implementation MyCustomView
@synthesize myLabel = _myLabel;
- (id)init
{
self = [super init];
if (self) {
_myLabel = [UILabel new];
if(_textView){
_myLabel.highlightedTextColor = [UIColor whiteColor];
_myLabel.translatesAutoresizingMaskIntoConstraints = NO;
_myLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myLabel.numberOfLines = 0;
_myLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_myLabel];
}
}
return self;
}
@end
如果我在init
-函数中将preferredMaxLayoutWidth
设置为显式值,则标签是多行的
有人知道我做错了什么吗
提前谢谢 如果看不到为自定义视图设置的所有约束以及包含它的superview,就很难确定问题所在,我建议您从viewDidLayoutSubviews中的视图控制器视图开始打印整个视图层次的所有图幅,并确定标签及其超级视图的图幅设置是否正确 我在动态标签大小和滚动视图方面遇到了类似的问题,因此我在这里创建了一个原型,可能对您也很有用:
- (void)layoutSubviews
{
[super layoutSubviews];
float width = _myLabel.frame.size.width;
_myLabel.preferredMaxLayoutWidth = width;
[super layoutSubviews];
}