如何为动态创建的按钮设置自动布局? [英] how to set auto layout for dynamically created buttons?

查看:53
本文介绍了如何为动态创建的按钮设置自动布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用滚动视图创建动态按钮和标签,我想为此设置自动布局.可能如何设置多个动态按钮.我搜索了许多教程,但没有获得设置自动布局多个动态按钮的任何示例.它可以工作,但所有按钮都设置在一起.意味着仅显示一个按钮和标签.但我正在搜索其显示正确的结果,但自动布局无法正常工作.什么问题

i am creating dynamic buttons and labels with scroll view now i want set auto layout for that . hows its possible set multiple dynamic buttons .i search many tutorial but not got any example for set auto layout multiple dynamic buttons.its work but all buttons are set together .means show only one button and label. but i am search its show correct result but auto layout is not work . what the problem

   -(void)DynamicButton:(NSMutableArray*)objectName
    {
     for(UIView *view in [scrollView subviews])
      {
        [view removeFromSuperview];
      }
      int yPossion = 100, xPossion = 44; int temp = 0;


      for (int i = 0; i<[objectName count]; i++)
     {
    SMSCategory *cat = [objectName objectAtIndex:i];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setTag:i];
    [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)];
    }

    UILabel *label =  [[UILabel alloc] init];
    [label setTranslatesAutoresizingMaskIntoConstraints:YES];

    [label setText:cat.Name];
    [label setTextColor:[UIColor blackColor]];
    label.font = [UIFont systemFontOfSize:12];

    [label sizeToFit];

    [label setFrame:CGRectMake(4, 44, 70, 60)];
    [scrollView addSubview:label];
    [aButton addSubview:label];

   }
   }

   //Autolayout code 
   [aButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSDictionary *viewsDictionary = @{@"aButton":aButton};

    // 2. Define the button Sizes
    NSArray *aButton_constraint_H = [NSLayoutConstraint 
                                     constraintsWithVisualFormat:@"V:[aButton(60)]"
                                                                        options:0
                                                                        metrics:nil
                                                                         views:viewsDictionary];

    NSArray *aButton_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[aButton(70)]"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:viewsDictionary];
    [aButton addConstraints:aButton_constraint_H];
    [aButton addConstraints:aButton_constraint_V];


    // 3. Define the views Positions using options
     NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[aButton]"
                                                                        options:0
                                                                        metrics:nil
                                                                        views:viewsDictionary];

     NSArray *constraint_POS = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[aButton]"
                                                                       options:0
                                                                       metrics:nil views:viewsDictionary];

    [self.view addConstraints:constraint_POS_V];
    [self.view addConstraints:constraint_POS];


}

推荐答案

您需要以编程方式添加约束,类似于使用searchBar代码的约束.您是否只是从某个地方复制了该代码,甚至根本不了解它?

You need to add the constraints programmatically, similar to how you have with your searchBar code. Did you just copy that code from somewhere without even understanding it?

对此有很多教程,例如: https://medium.com/@jsleeuw/mastering-programmatic-auto-layout-b02ed2499d79

There are many many tutorials for this, such as: https://medium.com/@jsleeuw/mastering-programmatic-auto-layout-b02ed2499d79

您将需要使用以下内容创建所有autoLayout约束:

You will need to create all your autoLayout constraints with something like this:

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label
                                                      attribute:NSLayoutAttributeCenterX
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeCenterX
                                                     multiplier:1.0
                                                       constant:0.0]];

在您的代码中已经有一种视觉格式.

There is also the visual format which you already have a sample of in your code.

我还编辑了您的问题,以清理文本并格式化代码,以使其易于阅读.请注意,我所做的更改使人们更容易阅读您的代码.

I also edited your question to clean up the text and to format the code so it is readable. Please note the changes I made to make it easier for people to read your code.

这篇关于如何为动态创建的按钮设置自动布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆