我在我的应用中创建了83个动态按钮并为其设置标签。现在,我想为它设置自动布局。如何为此动态设置自动布局?
这是我为动态创建按钮和标签而实现的代码:
-(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)];
}
}
}
答案 0 :(得分:0)
为此使用自动布局时,您需要设置setTranslatesAutoresizingMaskIntoConstraints: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个像素并将它们垂直居中。我不会查看您的代码并将其全部更改为自动布局,但您在我的示例中看到如何使用自动布局开始布置对象
答案 1 :(得分:0)
for(int i = 0; i&lt; [list count]; i ++)
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;