When your container is a navigation controller, the default layout will start at the top of the navigation bar. That's why all UI elements drift up by 44pt. Add the following code to fix it - (void)viewDidLoad
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) { self.edgesForExtendedLayout = UIRectEdgeNone; }
I encountered a problem when I was doing UISearchBar and UISearchDisplayController today, and the position of the shadow part was deviated when I clicked the search bar.
As shown below:
I always thought it was very strange. I made a demo later, copied the same code and found that it was normal.
Then look at the code one by one and see the following:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) { self.edgesForExtendedLayout = UIRectEdgeNone; } }
Find the suspicious, Google's iOS 7 tutorial: let the program support both iOS 6 and iOS 7 , find the answer.
reason:
In iOS 7, Apple introduced a new property called [UIViewController setEdgesForExtendedLayout:]
, whose default value is UIRectEdgeAll
. When your container is a navigation controller, the default layout will start at the top of the navigation bar. That's why all UI elements drift up by 44pt.
A quick fix for this is - (void)viewDidLoad
to add the following line of code to the method:
self.edgesForExtendedLayout = UIRectEdgeNone;