2013-09-24 116 views
0

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

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]; 

回答

1

如果正下方的导航栏确保搜索栏是,我会使用自动布局像这样:

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]; 
+0

由于设置自定义背景。它在constraintsWithVisualFormat中给我一个错误。它说searchBar不是视图字典中的关键。我尝试了其他一些事情,并得到相同的错误。再次感谢您的帮助。 – user1681673

+0

再次尝试使用我添加的额外行。 – Byte

+0

我尝试了您的更改的代码。构建的代码,但视图看起来与图片相同。 – user1681673