2014-10-08 57 views
0

我現在創建了83個按鈕動態現在我想爲它設置自動佈局。如何 我可以編碼? 我實現了以下代碼以生成動態按鈕。如何在iOS中爲多個動態按鈕添加自動佈局?

-(void)DynamicButton 
    { 

    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<83; i++){ 

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [aButton setTranslatesAutoresizingMaskIntoConstraints:YES]; 

    [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)]; 
    } 

     } 


    } 

how to add auto layout for multiple dynamic buttons in iOS? 
+0

列明智的三列 – chetan74 2014-10-08 11:22:20

+0

我想要的按鈕? – chetan74 2014-10-08 11:30:05

+0

如何從 – chetan74 2014-10-09 04:22:58

回答

0

我已經做了標籤: 完成純佈局。

APP_DELEGATE.labelWidth = 0; 
for (NSInteger i = 0;i < self.industry.numberOfColumns-1; i++) { 

     UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:16]; 

     NSDictionary *userAttributes = @{NSFontAttributeName: font, 
             NSForegroundColorAttributeName: ECONOMY_TABLE_ROW_TEXT_COLOR}; 

     NSString *text = [NSString stringWithFormat:@"%@",[[self.industry.data objectAtIndex:APP_DELEGATE.rowCount] objectAtIndex:i]];    

     const CGSize textSize = [text sizeWithAttributes: userAttributes]; 

     [self addStringWithWidth:textSize.width withHeight:textSize.height 
        withLeftEdge:0 
         witText:text]; 
    } 


- (void)addStringWithWidth:(NSInteger)width 
      withHeight:(NSInteger)height 
      withLeftEdge:(NSInteger)leftEdge 
       witText:(NSString *)labelString { 


self.indicatorLabel = [UIUtils createLabelWithText:nil 
            withTextColorRGB:ECONOMY_TABLE_ROW_TEXT_COLOR 
           withTextAlignment:NSTextAlignmentRight 
              withFont:@"Helvetica Neue" 
             andFontSize:16]; 

[self.contentView addSubview:self.indicatorLabel]; 

self.indicatorLabel.lineBreakMode = NSLineBreakByWordWrapping; 

self.indicatorLabel.numberOfLines = 0; 

[self.indicatorLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10.0f]; 
[self.indicatorLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10.0f]; 
[self.indicatorLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20+APP_DELEGATE.labelWidth]; 
[self.indicatorLabel autoSetDimension:ALDimensionWidth toSize:self.width]; 

self.indicatorLabel.text = labelString; 

APP_DELEGATE.labelWidth = APP_DELEGATE.labelWidth + self.width+20; 

} 
+0

你能幫助的按鈕代碼 – chetan74 2014-10-08 11:31:15