Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get *actual* frame size for UIScrollView in UINavigationController in iOS 7?

So it appears that the view frame in navigation controllers iOS 7 is defined rather differently (in my opinion, strangely) as compared to iOS 7...

EXAMPLE: Imagine a UIViewController that contains a UIScrollView (self.scrollView) that fills the entirety of self.view and with struts and springs so that it always fills self.view. Now embed that UIViewController in a UINavigationController with the navigation bar kept visible.

In iOS 6, I observe the following:

  • self.view.frame.origin = (0.0, X) where X = height of status bar (that contains time, signal bars, etc. = 20.0)
  • self.view.frame.size.height = self.view.bounds.size.height = overall height minus self.navigationController.navigationBar.frame.size.height
  • self.scrollView.frame.origin = self.scrollView.bounds.origin = (0.0, 0.0)

In iOS 7, I observe the following:

  • self.view.frame.origin = (0.0, 0.0)
  • self.view.frame.size.height = self.view.bounds.size.height = overall height (i.e., no reduction in height when embedded in navigation controller)
  • self.scrollView.bounds.origin = (0.0, -1 * self.navigationController.navigationBar.frame.size.height), but self.scrollView.frame.origin = (0.0, 0.0)

My guess is that this is done so that content can continue underneath the navigation bar so that the navigation bar can display the slightest amount of blur + transparency a la iOS 7.

QUESTION: How can I get the actual size (width x height) of self.scrollView in iOS 7 when it's embedded in a navigation controller, given the observations above, since self.scrollView.bounds.origin == self.scrollView.contentOffset? Do I simply have to write different code for iOS 6 v. iOS 7 and detect which version is running?

I don't really want to assume that the screen is any certain size and hard-code it in, since that seems like bad form...


UPDATE 1: I've uploaded a sketch of what I'm trying to ask. Note that the correct answer in this case is 416.0, which is easy to obtain in iOS 6, but I'm not sure how to obtain it in iOS 7 without resorting to assumptions.

Screenshots, iOS 6 v. iOS 7 Layout

like image 497
Ken M. Haggerty Avatar asked Nov 07 '13 05:11

Ken M. Haggerty


1 Answers

What is happening is the your UITableView is automatically being adjusted so that the content will be visible behind the UINavigationBar on iOS 7. This is done by changing the contentInset property found in UIScrollView. So to get the frame excluding the part behind the nav/status bar, you just need to account for the contentInset.

CGRect frame = self.tableView.frame;
frame.origin.y += self.tableView.contentInset.top
frame.size.height -= self.tableView.contentInset.top;

This code will also work on iOS 6 because self.tableView.contentInset.top will default to 0.

like image 155
Craig Siemens Avatar answered Oct 20 '22 11:10

Craig Siemens