删除 UISearchBar 左右两侧的空格
Removing the blank spaces on left and right side of the UISearchBar
我正在创建一个 UI 组件,它应该在下面显示一个 UILabel
和一个 UISearchBar
。
但是,我无法对齐它们,UISearchBar
总是在左侧和右侧都有额外的 space(突出显示为红色)。
我试图通过布局锚点直接设置 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 的方法 - 工作正常,但在第一次点击后中断:
UISearchBar 有这个 属性 searchFieldBackgroundPositionAdjustment:UIOffset
,您可以将其设置为 UIOffsetMake(-8,0)
以使文本字段的左侧与搜索栏的左边缘对齐。
在完整搜索栏应位于的位置创建一个 UIView,并将搜索栏设为子视图。将搜索栏的右边缘设置为超出它的超级视图,直到右边缘与超级视图对齐,它应该多出大约 16 点。确保 superview 的 属性 clipsToBounds
设置为 true,搜索栏背景的额外 16 点将被剪裁。
我正在创建一个 UI 组件,它应该在下面显示一个 UILabel
和一个 UISearchBar
。
但是,我无法对齐它们,UISearchBar
总是在左侧和右侧都有额外的 space(突出显示为红色)。
我试图通过布局锚点直接设置 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 的方法 - 工作正常,但在第一次点击后中断:
UISearchBar 有这个 属性 searchFieldBackgroundPositionAdjustment:UIOffset
,您可以将其设置为 UIOffsetMake(-8,0)
以使文本字段的左侧与搜索栏的左边缘对齐。
在完整搜索栏应位于的位置创建一个 UIView,并将搜索栏设为子视图。将搜索栏的右边缘设置为超出它的超级视图,直到右边缘与超级视图对齐,它应该多出大约 16 点。确保 superview 的 属性 clipsToBounds
设置为 true,搜索栏背景的额外 16 点将被剪裁。