Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 Table view fail to auto adjust content inset

I am transiting my project to iOS7. I am facing a strange problem related to the translucent navigation bar.

I have a view controller and it has a tableview as subview (let's call it ControllerA) . I init a new uinavigationcontroller with the controllerA and present it modally using presentviewcontroller. The presented view controller's table view is blocked by the navigation bar. I set the automaticallyAdjustsScrollViewInsets to YES but the result did not change. I knew I can set the edgesForExtendedLayout to UIRectEdgeNone, but it will make the navigation bar no more translucent.

Table view get blocked

After that, I tried to create a new view controller for testing. It contains almost the same elements. But the result is much different. The table view content does not get blocked.

enter image description here

Conclusion

  1. Two View Controllers' automaticallyAdjustsScrollViewInsets set to YES
  2. The project is not using storyboard
  3. The first one is created at Xcode 4.6, The second one is newly created on Xcode 5
  4. I have compared two classes xib and code, not much different
like image 485
Tony Fung Choi Fung Avatar asked Sep 30 '13 10:09

Tony Fung Choi Fung


2 Answers

I have found the answer on apple developer forum. There are two different case.

The first one, the view controller added is a UITableViewController. And the issue should not be appeared since apple will auto padding it.

The second one, the view controller is NOT a UITableViewController. And in the view hierarchy, it contains a UITableView. In this case, if the UITableview(or ScrollView) is the viewController's mainview or the first subview of the mainview, it will work. Otherwise, the view controller doesn't know which scroll view to padding and it will happen the issue.

In my case, the view controller is the second one. And there is a background image view as the first subview of the main view. So, it fails.

Here is the Apple developer forum link (need developer account to access): https://devforums.apple.com/message/900138#900138

like image 126
Tony Fung Choi Fung Avatar answered Sep 19 '22 18:09

Tony Fung Choi Fung


If you want the view to underlap the navigation bar, but also want it positioned so the top of the scrollview's content is positioned below the navigation bar by default, you can add a top inset manually once the view is laid out. This is essentially what the view layout system does when the top-level view is a scroll view.

-(void)viewDidLayoutSubviews {     if ([self respondsToSelector:@selector(topLayoutGuide)]) {         UIEdgeInsets currentInsets = self.scrollView.contentInset;         self.scrollView.contentInset = (UIEdgeInsets){             .top = self.topLayoutGuide.length,             .bottom = currentInsets.bottom,             .left = currentInsets.left,             .right = currentInsets.right         };     } } 
like image 24
Christopher Pickslay Avatar answered Sep 17 '22 18:09

Christopher Pickslay