Hi i am very new for ios. I am creating a project in which i have to deal with number of labels on various screens so i have to created one separate class which was inherited from UILabel
. I have set all label properties in that class.
But when i run the app. It shows exception like:
'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
The mas_makeConstraints
call should be made after label is added to view. Remove this call from +(id)getLabel
and add at the time of creation of label.
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);
}];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.