12

可能的重复:
自动布局甚至间距

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

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

滚动视图

没有那么多按钮:

视图不滚动

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

现在,我正在使用以下约束( self 是 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;
}

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

UIView 作为父视图

有任何想法吗?

4

1 回答 1

20
- (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 和 button3 之间的距离相同。

肖像 景观

于 2012-12-16T19:31:14.167 回答