今天在做UISearchBar,UISearchDisplayController时遇到了一个问题,在点击搜索栏时阴影部分的位置出现偏差
如下图:
始终觉得很奇怪,后面单独做了一个demo,将同样的代码拷过去发现显示正常的。
然后再逐一查看代码看到如下:
[objc] view plain copy print?
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- if (OSVersionIsAtLeastiOS7()) {
- if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
- {
- self.edgesForExtendedLayout = UIRectEdgeNone;
- }
- }
- }
发现可疑之处,Google之iOS 7 教程:让程序同时支持iOS 6和iOS 7,找到答案。
原因:
[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll
。当你的容器是navigation
controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。
- (void)viewDidLoad中添加如下一行代码:
1 |
|
这样问题就修复了。
2:可以用
self.nabigationcontroller.navigationbar.translucent = yes;
3:可以用
解决冲突 禁止系统偏移 vc.automaticallyadjustsScrollviewInsets =NO
其实这一切都是automaticallyAdjustsScrollViewInsets在作怪,我们可以先看一下官方文档中对它的描述:
automaticallyAdjustsScrollViewInsets
Specifies whether or not the view controller should automatically adjust its scroll view insets.
@property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets
Discussion
Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set toNO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.
Availability
- Available in iOS 7.0 and later.
Declared In
UIViewController.h
哈哈,由此可见,当我们一个界面有多个tableView之类的,要将它设置为NO,完全由自己手动来布局,就不会错乱了.