如何为iOS中的多个动态按钮添加自动布局?

时间:2014-10-08 10:56:31

标签: ios objective-c xcode6

我动态创建了83个按钮,现在我想为此设置自动布局。怎么样         我可以为此编码吗?         我按照代码实现了生成动态按钮。

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

1 个答案:

答案 0 :(得分: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;

}