如何在iOS中设置多个动态按钮和标签的自动布局?
我正在我的应用程序中创建83个动态按钮,并为此设置标签。现在,我想为相同的设置自动布局。如何为此动态设置自动布局 这是我为动态创建按钮和标签而实现的代码:如何在iOS中设置多个动态按钮和标签的自动布局?,ios,objective-c,xcode,autolayout,Ios,Objective C,Xcode,Autolayout,我正在我的应用程序中创建83个动态按钮,并为此设置标签。现在,我想为相同的设置自动布局。如何为此动态设置自动布局 这是我为动态创建按钮和标签而实现的代码: -(void)DynamicButton { //Reading Data From database NSMutableArray *objectName = [[NSMutableArray alloc] init]; NSArray *docPaths = NSSearchPathForDirectoriesIn
-(void)DynamicButton
{
//Reading Data From database
NSMutableArray *objectName = [[NSMutableArray alloc] init];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [docPaths objectAtIndex:0];
self.databasePath = [documentDir stringByAppendingPathComponent:@"SMS.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:self.databasePath];
[database setLogsErrors:TRUE];
[database open];
//FMResultSet *results = [database executeQuery:@"SELECT * FROM SMSCategory"];
NSString *anQuery = [[NSString alloc]initWithFormat:@"SELECT *FROM SMSCategory"];
FMResultSet *results = [database executeQuery:anQuery];
while ([results next]) {
SMSCat1 *cat = [[SMSCat1 alloc]init];
cat.Id = [results stringForColumn:@"Id"];
cat.Name = [results stringForColumn:@"Name"];
cat.IsActive = [results stringForColumn:@"IsActive"];
[objectName addObject:cat];
}
[database close];
//Adding dynamic buttons
int yPossion = 150, xPossion = 44; int temp = 0;
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
for (int i = 0; i < [objectName count]; i++) {
SMSCat1 *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
//Label Dynamic Code
UILabel *label = [[UILabel alloc] init];
[label setText:cat.Name];
[label setTextColor:[UIColor brownColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(5, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:@"icon-menu.png"] forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted = YES;
[scrollView addSubview:aButton];
xPossion += aButton.frame.size.width + 35;
temp++;
if (temp == 3) {
yPossion = aButton.frame.origin.y + aButton.frame.size.height + 20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width - 15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, yPossion - 50)];
}
}
}
-(无效)动态按钮
{
//从数据库读取数据
NSMutableArray*objectName=[[NSMutableArray alloc]init];
NSArray*docpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString*documentDir=[docPaths objectAtIndex:0];
self.databasePath=[documentDir stringByAppendingPathComponent:@“SMS.sqlite”];
FMDatabase*database=[FMDatabase databaseWithPath:self.databasePath];
[数据库设置日志错误:TRUE];
[数据库开放];
//FMResultSet*results=[数据库执行任务:@“从SMSCategory中选择*”;
NSString*anQuery=[[NSString alloc]initWithFormat:@“从SMSCategory中选择*”;
FMResultSet*results=[database executeQuery:anQuery];
而([下一步结果]){
SMSCat1*cat=[[SMSCat1 alloc]init];
cat.Id=[results stringForColumn:@“Id”];
cat.Name=[results stringForColumn:@“Name”];
cat.IsActive=[results stringForColumn:@“IsActive”];
[objectName addObject:cat];
}
[数据库关闭];
//添加动态按钮
int-yposion=150,xposion=44;int-temp=0;
UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
对于(int i=0;i<[objectName count];i++){
SMSCat1*cat=[objectName objectAtIndex:i];
UIButton*aButton=[UIButton按钮类型:UIButtonTypeCustom];
[aButton SetTranslates自动调整大小GMaskintoConstraints:是];
//标签动态代码
UILabel*label=[[UILabel alloc]init];
[标签设置文本:类别名称];
[标签设置颜色:[UIColor BROWNCOCLOR]];
label.font=[UIFont systemFontOfSize:12];
[标签尺寸合适];
[标签设置框:CGRectMake(5,44,70,60)];
[滚动视图添加子视图:标签];
[阿布顿添加子视图:标签];
[aButton setBackgroundColor:[UIColor blackColor];
[aButton setBackgroundImage:[UIImage ImageName:@“icon menu.png”]用于状态:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:@“%d”,i]用于状态:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion,yPossion,70,60)];
aButton.highlighted=是;
[滚动视图添加子视图:aButton];
xPossion+=邻接框架尺寸宽度+35;
temp++;
如果(温度=3){
yposion=aButton.frame.origin.y+aButton.frame.size.height+20;
温度=0;
xPossion=44;
yPossion+=邻接框架尺寸宽度-15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,yposion-50)];
}
}
}
为此使用自动布局时,您希望设置设置TranslatesAutoResizengMaskinToConstraints:NO
。这允许您使用自己的约束设置对象的位置。使用“自动布局”放置对象时,您并不真正关心框架。要相对于对象或其父视图周围的对象放置对象。例如,您可以执行以下操作:
NSDictionary* views = @{@"label1":label1,@"button1":button};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[label1]-(10)-[button1]" options:0 metrics:nil views:views]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:button attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
这将使两个元素相距10像素,并垂直居中。我不会浏览您的代码并将其全部更改为自动布局,但在我的示例中,您会看到如何使用自动布局开始布局对象(int I=0;我想为动态多按钮设置自动布局,但如何设置多个动态按钮?使用循环并使用X设置大小。工作正常。
id val=[list objectAtIndex:i];
CGFloat val1=[val floatValue];
UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 0,107,40)];
newLabel.text=[NSString stringWithFormat:@"%@",list[i]];
newLabel.textAlignment = NSTextAlignmentCenter;
newLabel.backgroundColor = [UIColor greenColor];
newLabel.layer.borderColor = [UIColor whiteColor].CGColor;
newLabel.layer.borderWidth = 2;
[self.view addSubview:newLabel];
x=x+newLabel.frame.size.width;