【问题标题】:Adding a button to the right side of a UISearchBar with AutoLayout: "Auto Layout still required after executing -layoutSubviews"使用 AutoLayout 在 UISearchBar 右侧添加按钮:“执行 -layoutSubviews 后仍需要自动布局”
【发布时间】:2013-07-28 17:39:17
【问题描述】:

在 ios6 上,我正在尝试执行以下操作:

UIButton *shortcutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[shortcutButton setTitle:@"Shortcut" forState:UIControlStateNormal];
[shortcutButton addTarget:self action:@selector(createShortcut) forControlEvents:UIControlEventTouchUpInside];
[shortcutButton setFrame:CGRectMake(0, 0, 70, 20)];
[self.searchBar addSubview:shortcutButton];

[shortcutButton setTranslatesAutoresizingMaskIntoConstraints:NO];

NSMutableArray *searchBarConstraints = [NSMutableArray array];

[searchBarConstraints addObject: [NSLayoutConstraint constraintWithItem:self.searchBar
                                                              attribute:NSLayoutAttributeCenterY
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:shortcutButton
                                                              attribute:NSLayoutAttributeCenterY
                                                             multiplier:1
                                                               constant:0]];

[searchBarConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[shortcutButton]-35-|"
                                                                                  options:0
                                                                                  metrics:nil
                                                                                    views:@{ @"shortcutButton" : shortcutButton  ,  @"view" : self.view }]];
[self.searchBar addConstraints:searchBarConstraints];

这会导致崩溃并出现错误:

*** Assertion failure in -[UISearchBar layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2380.17/UIView.m:5776
Uncaught exception: Auto Layout still required after executing -layoutSubviews. UISearchBar's implementation of -layoutSubviews needs to call super.

我正在以编程方式创建一个 UISearchbar 并尝试在搜索栏的右侧插入一个 UIButton。有没有办法处理这个?谢谢!

【问题讨论】:

    标签: ios uibutton uisearchbar autolayout


    【解决方案1】:

    yahuuu 我找到了解决方案。 尝试将您设置的约束逻辑放入 didMoveToSuperview 方法中。 我认为这与在层次结构上加载所有视图之前设置约束有关。 或者只是调用 [super layoutSubviews];设置约束后

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 2012-09-18
      • 2015-11-10
      • 2015-07-09
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多