【问题标题】:iOS Autolayout and UIToolbar/UIBarButtonItemsiOS 自动布局和 UIToolbar/UIBarButtonItems
【发布时间】:2013-02-05 14:22:28
【问题描述】:

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

AutoLayout 之前,我会使用autoresizingmasks 完成此操作。

UIToolbars/UINavigationBars 中是否不允许有约束?

在使用自动布局时还能如何实现?

【问题讨论】:

  • 我以前做过一些类似的事情,虽然我基本上只是等待视图的大小发生变化,然后相应地更新目标视图的框架。如果事实证明无法自动执行此操作,那始终是一种选择。
  • 你能发一张图片吗?
  • 我愿意,但我最终放弃了 AutoLayout 并重新使用自动调整掩码。使用该模型效果很好。
  • @Brian 你建议如何观察视图大小的变化?
  • @devios 不确定您的用例,但 layoutSubViews 可能是您想要的。这将在方向改变等时调用。developer.apple.com/library/ios/documentation/UIKit/Reference/…

标签: ios uibarbuttonitem uitoolbar autolayout autoresizingmask


【解决方案1】:

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

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

在自动布局可以与 UIBarButtonItems 一起使用之前,照你的做。

您的替代方法是使用仅基于 UIViews 的小部件来滚动您自己的工具栏。

【讨论】:

  • 知道 Apple 是否正在努力解决这个问题吗?如果我必须在不同的地方使用 AL 和旧方法,那么不一致会被打破,这让我很烦恼......
【解决方案2】:

这也可以直接从情节提要中完成。

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

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

【讨论】:

    【解决方案3】:

    您至少可以在代码中做到这一点;我是那种放弃 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;
    }
    

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

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

    似乎对我有用。不过,我没有添加任何UIBarButtonItems,所以你的里程数因人而异。

    【讨论】:

    • 如果您尝试使用约束重新定位现有工具栏,您会发现 NSLayoutAttributeCenterX 约束非常有用。
    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多