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