1. <tfoot id='IZlZM'></tfoot>
  2. <i id='IZlZM'><tr id='IZlZM'><dt id='IZlZM'><q id='IZlZM'><span id='IZlZM'><b id='IZlZM'><form id='IZlZM'><ins id='IZlZM'></ins><ul id='IZlZM'></ul><sub id='IZlZM'></sub></form><legend id='IZlZM'></legend><bdo id='IZlZM'><pre id='IZlZM'><center id='IZlZM'></center></pre></bdo></b><th id='IZlZM'></th></span></q></dt></tr></i><div id='IZlZM'><tfoot id='IZlZM'></tfoot><dl id='IZlZM'><fieldset id='IZlZM'></fieldset></dl></div>

    • <bdo id='IZlZM'></bdo><ul id='IZlZM'></ul>
    <legend id='IZlZM'><style id='IZlZM'><dir id='IZlZM'><q id='IZlZM'></q></dir></style></legend>

    1. <small id='IZlZM'></small><noframes id='IZlZM'>

      如何使用 AutoLayout 将 UIButtons 定位在水平线中(环绕,左对齐)?

      时间:2023-09-10

        <tbody id='9yEh3'></tbody>
          • <i id='9yEh3'><tr id='9yEh3'><dt id='9yEh3'><q id='9yEh3'><span id='9yEh3'><b id='9yEh3'><form id='9yEh3'><ins id='9yEh3'></ins><ul id='9yEh3'></ul><sub id='9yEh3'></sub></form><legend id='9yEh3'></legend><bdo id='9yEh3'><pre id='9yEh3'><center id='9yEh3'></center></pre></bdo></b><th id='9yEh3'></th></span></q></dt></tr></i><div id='9yEh3'><tfoot id='9yEh3'></tfoot><dl id='9yEh3'><fieldset id='9yEh3'></fieldset></dl></div>
            <legend id='9yEh3'><style id='9yEh3'><dir id='9yEh3'><q id='9yEh3'></q></dir></style></legend>
              <bdo id='9yEh3'></bdo><ul id='9yEh3'></ul>
            • <small id='9yEh3'></small><noframes id='9yEh3'>

              1. <tfoot id='9yEh3'></tfoot>
                本文介绍了如何使用 AutoLayout 将 UIButtons 定位在水平线中(环绕,左对齐)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要在我的应用程序(iOS 6.0 及更高版本)中以编程方式创建几个不同宽度的 UIButton.

                I need to create a couple of UIButtons with various widths programmatically in my app (iOS 6.0 and above).

                我想以环绕"样式显示按钮:从左边缘开始,每个按钮应水平放置(按定义的顺序),如果按钮不适合当前"line",它应该在前一行下方的左边缘开始新的一行.

                I want to display the buttons in a "wrap around" style: Starting from the left edge, each button should be positioned next to each other horizontally (in a defined order), and if a button does not fit in the current "line", it should start a new line on the left edge below the previous line.

                注意:我不想要表格/网格,因为按钮有不同的宽度,我希望有一个彼此相邻.

                Note: I don't want a table/grid, since the buttons have different widths, and I want to have one right next to each other.

                我可以手动计算代码中每个按钮的框架,但我应该使用 AutoLayout(使用以编程方式创建的 NSLayoutConstraints)来代替吗?我究竟需要如何设置它?

                I could manually calculate the frame of each button in my code, but should I use AutoLayout (with programmatically created NSLayoutConstraints) instead? How exactly would I need to set it up?

                通读 "iOS 6 的第 4 章中级自动布局"by Tutorials" 我不确定使用纯 AutoLayout 是否可以实现我需要的这种环绕"功能.

                After reading through Chapter 4 "Intermediate Auto Layout" of "iOS 6 by Tutorials" I am not sure whether using pure AutoLayout could implement this "wrap around" functionality I require.

                推荐答案

                我目前的解决方案是这样的:没有 AutoLayout,但是为每种情况手动设置正确的约束(第一个按钮,新行中最左边的按钮,任何其他按钮).

                My current solution looks like this: No AutoLayout, but manually setting the correct constraints for each case (first button, leftmost button in a new line, any other button).

                (我的猜测是直接为每个按钮设置框架会导致比使用 NSLayoutConstraints 更具可读性的代码,无论如何)

                (My guess is that setting the frame for each button directly would result in more readable code than using NSLayoutConstraints, anyway)

                NSArray *texts = @[ @"A", @"Short", @"Button", @"Longer Button", @"Very Long Button", @"Short", @"More Button", @"Any Key"];
                
                int indexOfLeftmostButtonOnCurrentLine = 0;
                NSMutableArray *buttons = [[NSMutableArray alloc] init];
                float runningWidth = 0.0f;
                float maxWidth = 300.0f;
                float horizontalSpaceBetweenButtons = 10.0f;
                float verticalSpaceBetweenButtons = 10.0f;
                
                for (int i=0; i<texts.count; i++) {
                    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    [button setTitle:[texts objectAtIndex:i] forState:UIControlStateNormal];
                    [button sizeToFit];
                    button.translatesAutoresizingMaskIntoConstraints = NO;
                
                    [self.view addSubview:button];
                
                    // check if first button or button would exceed maxWidth
                    if ((i == 0) || (runningWidth + button.frame.size.width > maxWidth)) {
                        // wrap around into next line
                        runningWidth = button.frame.size.width;
                
                        if (i== 0) {
                            // first button (top left)
                            // horizontal position: same as previous leftmost button (on line above)
                            NSLayoutConstraint *horizontalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:horizontalSpaceBetweenButtons];
                            [self.view addConstraint:horizontalConstraint];
                
                            // vertical position:
                            NSLayoutConstraint *verticalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop              multiplier:1.0f constant:verticalSpaceBetweenButtons];
                            [self.view addConstraint:verticalConstraint];
                
                
                        } else {
                            // put it in new line
                            UIButton *previousLeftmostButton = [buttons objectAtIndex:indexOfLeftmostButtonOnCurrentLine];
                
                            // horizontal position: same as previous leftmost button (on line above)
                            NSLayoutConstraint *horizontalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:previousLeftmostButton attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f];
                            [self.view addConstraint:horizontalConstraint];
                
                            // vertical position:
                            NSLayoutConstraint *verticalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousLeftmostButton attribute:NSLayoutAttributeBottom multiplier:1.0f constant:verticalSpaceBetweenButtons];
                            [self.view addConstraint:verticalConstraint];
                
                            indexOfLeftmostButtonOnCurrentLine = i;
                        }
                    } else {
                        // put it right from previous buttom
                        runningWidth += button.frame.size.width + horizontalSpaceBetweenButtons;
                
                        UIButton *previousButton = [buttons objectAtIndex:(i-1)];
                
                        // horizontal position: right from previous button
                        NSLayoutConstraint *horizontalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:previousButton attribute:NSLayoutAttributeRight multiplier:1.0f constant:horizontalSpaceBetweenButtons];
                        [self.view addConstraint:horizontalConstraint];
                
                        // vertical position same as previous button
                        NSLayoutConstraint *verticalConstraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousButton attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
                        [self.view addConstraint:verticalConstraint];
                    }
                
                    [buttons addObject:button];
                }
                

                这篇关于如何使用 AutoLayout 将 UIButtons 定位在水平线中(环绕,左对齐)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:UITableViewCellContentView 高度怎么可能与 heightForRowAtIndexPath 不 下一篇:以编程方式设置 AutoLayout 大小类?

                相关文章

                • <bdo id='xnoeb'></bdo><ul id='xnoeb'></ul>
              2. <i id='xnoeb'><tr id='xnoeb'><dt id='xnoeb'><q id='xnoeb'><span id='xnoeb'><b id='xnoeb'><form id='xnoeb'><ins id='xnoeb'></ins><ul id='xnoeb'></ul><sub id='xnoeb'></sub></form><legend id='xnoeb'></legend><bdo id='xnoeb'><pre id='xnoeb'><center id='xnoeb'></center></pre></bdo></b><th id='xnoeb'></th></span></q></dt></tr></i><div id='xnoeb'><tfoot id='xnoeb'></tfoot><dl id='xnoeb'><fieldset id='xnoeb'></fieldset></dl></div>

                1. <legend id='xnoeb'><style id='xnoeb'><dir id='xnoeb'><q id='xnoeb'></q></dir></style></legend>

                  <small id='xnoeb'></small><noframes id='xnoeb'>

                  <tfoot id='xnoeb'></tfoot>