2014-10-10 50 views
2

我在我的應用程序中創建83個動態按鈕併爲其設置標籤。現在,我想爲相同的設置自動佈局。我怎樣才能爲此動態設置自動佈局?如何在iOS中爲多個動態按鈕和labell設置自動佈局?

這是我創建按鈕和標籤動態的實現代碼:

-(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

我想設置爲動態多個按鈕 – 2014-10-10 09:33:08

回答

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個像素,垂直居中他們。我不會通過你的代碼,並將其全部更改爲自動佈局,但你在我的示例中看到如何開始用自動佈局佈局對象

+0

自動佈局,如果你知道該怎麼做,請告訴我...... – 2014-10-10 09:54:05

+0

但如何爲多個動態按鈕設置? – 2014-10-17 09:57:33

0

for(int i = 0; i < [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; 
+0

用於循環和使用X ..設置大小。它的工作很好。 – 2016-07-28 08:40:44