ios - UINavigationBar 和 UISearchBar 之间的小差距

标签 ios interface-builder uinavigationbar uisearchbar

我以编程方式向我的应用程序添加了一个 UISearch,并且在界面生成器中添加了 UINavigation 栏。当我添加搜索栏时,它在搜索栏和导航栏之间留下了一个小间隙。有没有办法将它们合并在一起?谢谢。

Screenshot

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0,    self.view.bounds.size.width, 44.0)];
self.searchBar.delegate = self;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.searchBar.showsCancelButton = NO;
[self.view addSubview:self.searchBar];

最佳答案

如果确保 SearchBar 位于导航栏的正下方,我将使用自动布局,如下所示:

self.searchBar = [[UISearchBar alloc] init];
self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
self.searchBar.delegate = self;
self.searchBar.showsCancelButton = NO;
[self.view addSubview:self.searchBar];

id topLayoutGuide = self.topLayoutGuide;
UISearchBar *searchBar = self.searchBar; //updated

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(searchBar)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][searchBar]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(searchBar, topLayoutGuide)]];

编辑:

我查了一下,发现这不是一个间隙。苹果公司用它来划分事物的位置。您可以使用多种选择

1.) 搜索并销毁 - 找到 UIImageView,并将其从栏中删除。
Remove UIToolbar hairline in iOS 7

2.) 为您的栏设置自定义背景

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];

关于ios - UINavigationBar 和 UISearchBar 之间的小差距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989409/

相关文章:

ios - 使用 ImageView 与程序化 Segue 进行挑战

cocoa - 在 Mac 上的 Interface Builder 中获取标准系统按钮

iphone - ios7 导航栏左侧 UIBarButtonItem 问题

iphone - iOS Quartz 在 pdf 中嵌入字体

ios - 使用 Swift 分析录制的音频文件以进行语音转文本

ios - 如何将类属性与枚举匹配?

objective-c - 不使用 Interface Builder 的 Xcode 编程

iOS 7 backBarButtonItem 隐藏

iOS 5 [UINavigationBar appearance] setBackgroundImage 不工作

ios - 对于这种情况,我应该使用 Google 应用内购买还是 Google Pay?