• <bdo id='ZeRG6'></bdo><ul id='ZeRG6'></ul>

      <tfoot id='ZeRG6'></tfoot>
    1. <small id='ZeRG6'></small><noframes id='ZeRG6'>

    2. <legend id='ZeRG6'><style id='ZeRG6'><dir id='ZeRG6'><q id='ZeRG6'></q></dir></style></legend>
        <i id='ZeRG6'><tr id='ZeRG6'><dt id='ZeRG6'><q id='ZeRG6'><span id='ZeRG6'><b id='ZeRG6'><form id='ZeRG6'><ins id='ZeRG6'></ins><ul id='ZeRG6'></ul><sub id='ZeRG6'></sub></form><legend id='ZeRG6'></legend><bdo id='ZeRG6'><pre id='ZeRG6'><center id='ZeRG6'></center></pre></bdo></b><th id='ZeRG6'></th></span></q></dt></tr></i><div id='ZeRG6'><tfoot id='ZeRG6'></tfoot><dl id='ZeRG6'><fieldset id='ZeRG6'></fieldset></dl></div>
      1. iOS 自动布局:相等的空间以适应超级视图的宽度

        iOS Auto Layout: Equal Spaces to Fit Superviews Width(iOS 自动布局:相等的空间以适应超级视图的宽度)

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

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

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

                  本文介绍了iOS 自动布局:相等的空间以适应超级视图的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  可能重复:
                  自动布局均匀间距

                  我正在尝试创建一个带有按钮的可滚动条(类似于 UISegmentedControl).超级视图是一个 UIScrollView.一旦按钮不适合滚动视图,滚动视图应该是可滚动的.到目前为止,几乎一切正常:

                  I'm trying to create a scrollable bar with buttons (similar to a UISegmentedControl). The superview is an UIScrollView. As soon as the buttons don't fit into the scrollview, the scrollview should be scrollable. So far, almost everything works fine:

                  有很多按钮(向右滚动):

                  With a lot of buttons (scrolled to the right):

                  按钮不多:

                  现在,我的目标是,如果所有按钮都有空间,它们应该均匀分布在整个 320 像素视图中.如何为按钮之间的空间定义约束?

                  Now, my goal is that if there is room for all buttons, they should be equally spread across the whole 320px view. How can I define constrains for the spaces in between the buttons?

                  现在,我正在使用以下约束(self 是 UIScrollView):

                  Right now, I'm using the following constraints (self is a UIScrollView):

                  UIView *firstButton = self.buttons[0];
                  
                  [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(5)-[firstButton]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(firstButton)]];
                  
                  UIView *lastButton = [self.buttons lastObject];
                  
                  [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[lastButton]-(5)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastButton)]];
                  
                  UIView *previousView = nil;
                  
                  for (UIView *view in self.buttons) {
                      if (previousView) {
                          [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[previousView]-(5)-[view]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(previousView, view)]];
                      }
                      previousView = view;
                  }
                  

                  如果我将超级视图的类型从 UIScrollView 更改为 UIView,我会得到以下结果,仍然不是我想要的,但至少它会寻找最后一个按钮将其绑定到右边缘的约束(有道理,滚动视图不会发生这种情况,因为内容大小是自动设置的):

                  If I change the type of the superview from UIScrollView to an UIView, I get the following result, still not what I want, but at least it looks for the constraint of the last button that ties it to the right edge (makes sense, that this doesn't happen for the scrollview, as the content size is automatically set):

                  有什么想法吗?

                  推荐答案

                  - (void) viewWillLayoutSubviews {
                      // UIButton *button1, *button2, *button3, *button 4 ;
                      // NSMutableArray *constraintsForButtons ;
                  
                      float unusedHorizontalSpace = self.view.bounds.size.width - button1.intrinsicContentSize.width - button2.intrinsicContentSize.width - button3.intrinsicContentSize.width - button4.intrinsicContentSize.width ;
                      NSNumber* spaceBetweenEachButton=  [NSNumber numberWithFloat: unusedHorizontalSpace / 5 ] ;
                  
                      [self.view removeConstraints:constraintsForButtons] ;
                      [constraintsForButtons removeAllObjects] ;
                      [constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|-(space)-[button1]-(space)-[button2]-(space)-[button3]-(space)-[button4]-(space)-|"
                                                                                                          options: NSLayoutFormatAlignAllCenterY
                                                                                                          metrics: @{@"space":spaceBetweenEachButton}
                                                                                                            views: NSDictionaryOfVariableBindings(button1,button2,button3,button4) ] ] ;
                      [constraintsForButtons addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[button1]"
                                                                                                          options: 0
                                                                                                          metrics: nil
                                                                                                            views: NSDictionaryOfVariableBindings(button1) ] ] ;
                      [self.view addConstraints:constraintsForButtons] ;
                  }
                  

                  这不像你的那么漂亮,它假设有 4 个按钮,但它们的间距相等.也就是说,按钮之间的空白空间都具有相同的宽度.这并不意味着button1和button2的NSLayoutAttributeLeading之间的距离与button2和button2之间的距离一样.button3 的.

                  This isn't as pretty as yours, and it assumes there are 4 buttons, but it equally spaces them. That is, the empty spaces between the buttons all have the same width. This does not mean that the distance between the NSLayoutAttributeLeading of button1 and button2 is the same as the distance between button2 & button3's.

                  这篇关于iOS 自动布局:相等的空间以适应超级视图的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS AutoLayout - get frame size width(iOS AutoLayout - 获取帧大小宽度)
                  Auto layout constraints issue on iOS7 in UITableViewCell(UITableViewCell中iOS7上的自动布局约束问题)
                  How to resize superview to fit all subviews with autolayout?(如何调整超级视图的大小以适应所有具有自动布局的子视图?)
                  Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5(自动布局中的弹簧:在 Xcode 5 中使用约束均匀分布视图)
                  reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling(具有动态单元格高度的 UITableView 的 reloadData() 导致跳跃滚动)
                  What is NSLayoutConstraint quot;UIView-Encapsulated-Layout-Heightquot; and how should I go about forcing it to recalculate cleanly?(什么是 NSLayoutConstraint“UIView-Encapsulated-Layout-Height?我应该如何强制它干净地重新计算?) - IT屋-程序员
                1. <legend id='ql9YX'><style id='ql9YX'><dir id='ql9YX'><q id='ql9YX'></q></dir></style></legend>
                    <tbody id='ql9YX'></tbody>

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

                        • <bdo id='ql9YX'></bdo><ul id='ql9YX'></ul>

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