嗨,我对ios很新。我正在创建一个项目,我必须处理各种屏幕上的标签数量,所以我必须创建一个从UILabel
继承的独立类。我在该类中设置了所有标签属性。
但是当我运行应用程序时。它显示例外:
'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]
#import "CustomLabel.h"
@implementation CustomLabel
+(id)getLabel {
CustomLabel *menu;
menu = [[self alloc] init];
[menu setBackgroundColor:[UIColor redColor]];
[menu setTextColor:[UIColor blackColor]];
menu.text = @"hjh";
[menu mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@200);
make.height.equalTo(@30);
make.left.equalTo(@10);
make.right.equalTo(@-10);
}];
return menu;
}
#import "MainViewController.h"
#import "CustomLabel.h"
@interface MainViewController ()
{
CustomLabel * mainLabel;
}
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
mainLabel = [CustomLabel getLabel];
[self.view addSubview:mainLabel];
}
@end
答案 0 :(得分:0)
在将标签添加到视图后,应进行mas_makeConstraints
调用。从+(id)getLabel
删除此调用,并在创建标签时添加。
CustomLabel* mainLabel = [CustomLabel getLabel];
[self.view addSubview:mainLabel];
[mainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(@200);
make.height.equalTo(@30);
make.left.equalTo(@10);
make.right.equalTo(@-10);
}];