【问题标题】:Removing the blank spaces on left and right side of the UISearchBar删除 UISearchBar 左右两边的空格
【发布时间】:2018-12-27 07:36:43
【问题描述】:

我正在创建一个 UI 组件,该组件应在下方显示 UILabel 和 UISearchBar。

但是,我无法对齐它们,UISearchBar 在左右两侧总是有额外的空间(红色突出显示)。

我试图通过布局锚点直接设置searchBarTextField的尺寸,但没有成功。

如果可能,我更愿意使用布局锚点。

SearchBar.m:

-(id) init {        
    self.titleLabel = [UILabel new];
    self.searchBar = self.searchController.searchBar;
    UIView *searchBarWrapper = [UIView new];

    [self.view addSubview:self.titleLabel];
    [self.view addSubview:searchBarWrapper];
    [searchBarWrapper addSubview:self.searchBar];

    [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [searchBarWrapper setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.searchBar setTranslatesAutoresizingMaskIntoConstraints:NO];

    //[self.titleLabel.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:1.0].active = YES;
    [self.titleLabel.heightAnchor constraintEqualToConstant:14.0].active = YES;
    [self.titleLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
    [self.titleLabel.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
    [self.titleLabel.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
    //[self.titleLabel.bottomAnchor constraintEqualToAnchor:searchBarTextField.topAnchor].active = YES;

    [searchBarWrapper.widthAnchor constraintEqualToAnchor:self.view.widthAnchor multiplier:1.0].active = YES;
    //[self.searchBar.heightAnchor constraintEqualToConstant:36.0].active = YES;
    [searchBarWrapper.topAnchor constraintEqualToAnchor:self.titleLabel.bottomAnchor].active = YES;
    [searchBarWrapper.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
    [searchBarWrapper.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
    [searchBarWrapper.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;

    [self.searchBar.topAnchor constraintEqualToAnchor:searchBarWrapper.topAnchor].active = YES;
    [self.searchBar.bottomAnchor constraintEqualToAnchor:searchBarWrapper.bottomAnchor].active = YES;
    [self.searchBar.leftAnchor constraintEqualToAnchor:searchBarWrapper.leftAnchor].active = YES;
    [self.searchBar.rightAnchor constraintEqualToAnchor:searchBarWrapper.rightAnchor constant:16.0].active = YES;

    return self;
}

UISearchBarTextField 嵌入在我无权访问的UIView 中。

约束:

@BenOng 的方法 - 效果很好,但在第一次点击后会中断:

【问题讨论】:

  • That 原始解决方案对我有用

标签: ios autolayout uisearchbar layout-anchor nslayoutanchor


【解决方案1】:

UISearchBar 有这个属性searchFieldBackgroundPositionAdjustment:UIOffset,您可以将其设置为UIOffsetMake(-8,0) 以使文本字段的左侧与搜索栏的左边缘对齐。

在完整搜索栏应位于的位置创建一个 UIView,并将搜索栏设为子视图。将搜索栏的右边缘设置在其超级视图之外,直到右边缘与超级视图对齐,它应该多出大约 16 个点。确保superview的属性clipsToBounds设置为true,搜索栏背景多出的16点会被剪掉。

【讨论】:

  • 设置UIOffsetMake(-8,0) 有助于解决左偏移的问题。但是将 UIView 设置为超级视图并不适合我。一开始它可以工作,但是在点击搜索栏后,它的布局会被重新计算,额外的 16 点空白。另外,您建议clipsToBounds,会使右侧的边缘不圆。
  • 您是否正确设置了约束? UISearchBar 的原始约束应应用于 UIView 并将搜索栏制成它的子视图,约束对齐尾随 -16。这应该将 textField 的后沿放在 superview 的尾部。
  • clipsToBound 应该只裁剪我们从超级视图中推出的额外空间。如果部分文本字段被剪裁,则可以尝试使用更大的值而不是 -16 作为尾随。 -16 只是基于间距的猜测,可能更小。
  • 我按照您的描述完成了所有步骤,搜索栏在呈现时看起来还不错。但是在第一次点击搜索栏后(仅是第一次),搜索字段会移位(向下约 8px,右侧缺少 16px)。然后我旋转设备以重新呈现搜索字段,之后可以按我想要的次数点击它,它工作正常。我在控制台中没有收到布局约束警告。
  • 您可以查看更新的代码和添加的截图。
猜你喜欢
  • 1970-01-01
  • 2014-07-09
  • 2015-12-01
  • 2021-05-11
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多