iOS 自动布局和 UIToolbar/UIBarButtonItems

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14982302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:22:33  来源:igfitidea点击:

iOS Autolayout and UIToolbar/UIBarButtonItems

iosuibarbuttonitemuitoolbarautolayoutautoresizingmask

提问by outerstorm

I have an iOS view with autolayout enabled and have a UIToolbarwith a UISearchBarand UISegmentControlcontained with the toolbar. I want the UISearchBarto have a flexible width so I need to add a constraint to force this, but from what I can tell you cannot add constraints to items in a UIToolbarin Interface Builder. The options are all disabled.

我有一个启用了自动布局的 iOS 视图,并且有UIToolbar一个UISearchBarUISegmentControl包含在工具栏中。我希望UISearchBar具有灵活的宽度,因此我需要添加一个约束来强制执行此操作,但是据我所知,您无法UIToolbar在 Interface Builder中向 a中的项目添加约束。所有选项都被禁用。

Before AutoLayoutI would accomplish this with autoresizingmasks.

AutoLayout我用autoresizingmasks.

Are constraints not allowed within UIToolbars/UINavigationBars?

内不允许有约束UIToolbars/UINavigationBars吗?

How else can this be accomplished when using autolayout?

使用自动布局时还可以如何实现?

回答by snorock

Autolayout constraints only work with UIViewsand their subclasses.

自动布局约束仅适用于UIViews及其子类。

While UIToolbarallows some UIViewbased items (such as UISearchBarand UISegmentedControl) they may have to coexist with UIBarButtonItemswhich do not inherit from UIView.

虽然UIToolbar允许某些UIView基于项目(例如UISearchBarUISegmentedControl),但它们可能必须与UIBarButtonItems不继承自 的项目共存UIView

Until autolayout can work with UIBarButtonItems, do as you have done.

在自动布局可以使用之前UIBarButtonItems,请按照您的方式进行操作。

Your alternative is to roll your own toolbar with widgets based only on UIViews.

您的替代方法是使用仅基于UIViews.

回答by Nycen

This can also be done right from a storyboard.

这也可以直接从故事板完成。

Just drag and drop items in the toolbar, and turn some of them into flexible or fixed space to get the desired effect. See the two examples below.

只需拖放工具栏中的项目,将其中的一些变成灵活或固定的空间即可获得所需的效果。请参阅下面的两个示例。

Evenly spaced

均匀分布的

Centered

居中

NB: this is a copy of my answer to Aligning UIToolBar items, I stumbbled upon both questions while looking for such a solution

注意:这是我对Aligning UIToolBar items 的回答的副本,我在寻找这样的解决方案时偶然发现了两个问题

回答by Ben Kreeger

You can do this in code, at least; I'm the type to forsake Interface Builder and go it in code anyway. IB seems to get in my way more often than not when it comes to adding or tweaking constraints. Here's what I've done in my custom UIToolbarsubclass's -initWithFrame:method.

你至少可以在代码中做到这一点;我是那种放弃 Interface Builder 并在代码中使用它的类型。在添加或调整约束时,IB 似乎经常妨碍我。这是我在自定义UIToolbar子类的-initWithFrame:方法中所做的。

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.label];

        [self addConstraint:[NSLayoutConstraint
                             constraintWithItem:self.label
                             attribute:NSLayoutAttributeCenterX
                             relatedBy:NSLayoutRelationEqual
                             toItem:self
                             attribute:NSLayoutAttributeCenterX
                             multiplier:1 constant:0]];
        [self addConstraint:[NSLayoutConstraint
                             constraintWithItem:self.label
                             attribute:NSLayoutAttributeCenterY
                             relatedBy:NSLayoutRelationEqual
                             toItem:self
                             attribute:NSLayoutAttributeCenterY
                             multiplier:1 constant:0]];
    }
    return self;
}

And since I like to lazy load as much as possible, here's my self.labelinstance variable (called when [self addSubview:self.label]gets messaged above).

由于我喜欢尽可能延迟加载,这是我的self.label实例变量(在[self addSubview:self.label]上面收到消息时调用)。

- (UILabel *)label {
    if (_label) return _label;
    _label = [UILabel new];
    _label.translatesAutoresizingMaskIntoConstraints = NO;
    _label.textAlignment = NSTextAlignmentCenter;
    return _label;
}

Seems to work for me. I'm not adding any UIBarButtonItems, though, so your mileage my vary.

似乎对我有用。UIBarButtonItems不过,我没有添加任何,因此您的里程数会有所不同。