ios - How to determine UINavigationBar size before device orientation changes -


i'm writing app in objective-c using xcode 6 , ios 8. app needs able deployed on iphone 5, 6, or 6+.

if want straight answering question, jump down last sentence. if want understand why have question do, or maybe how can alter ui layout in order solve problem way, read on.

in 1 of view controllers, have scroll view top constrained bottom of navigation bar, , bottom constrained top of table view. table view's bottom constrained bottom of view controller's main view (i.e. bottom of phone).

the scroll view contains subviews expand/contract when user taps on them. want scroll view grow subviews grow, don't want scroll view grow off screen because looks bad , because cause unsatisfiable constraints (the table view's top--which constrained bottom of scroll view--would cross below bottom--which constrained bottom of main view...this causes error). so, use following code make scroll view resize according subviews sizes without growing right off screen:

// max height before scroll view go off screen,  // mess table view's constraints , cause sorts of problems cgfloat maxheight = self.view.size.height                   - self.navigationcontroller.navigationbar.frame.size.height                   - [uiapplication sharedapplication].statusbarframe.size.height;  // height of subviews in scroll view. cgfloat height = _scrollcontentview.frame.size.height;  if (height > maxheight) {     height = maxheight; }  self.scrollviewheightconstraint.constant = height; 

now fun part. originally, called code re-evaluate , reset size of scroll view whenever rotated device portrait landscape, or vice versa. however, when rotate phone portrait landscape, getting constraints errors. determined because calling code after rotation, when main view's height smaller, scroll view's height still large (causing table view's top go below bottom, etc. explained before). so, moved code called before rotation (i called code in viewwilltransitionwithsize:withtransitioncoordinator: method). makes sense far.

however, now, problem navigation bar's height changes when rotation occurs, viewwilltransitionwithsize:... method not include details on change (it gives new size main view when rotation completed, not new size navigation bar well).

so, need someway determine new size of navigation bar before device's orientation changes (just can determine main view's new size before device's orientation changes using viewwilltransitionwithsize:... method).

any ideas? tia!

so, here's work around in simplest form:

/*  * method gets called when device rotate.  */ - (void)viewwilltransitiontosize:(cgsize)size withtransitioncoordinator:(id<uiviewcontrollertransitioncoordinator>)coordinator {     // set scroll view's height 0 avoid constraints errors described     // in question.     self.scrollviewheightconstraint.constant = 0; }  /*  * @ point when method gets called, device rotation has finished altering  * frames of views in view controller, layout has not finished  * nothing has changed on screen.  */ - (void)viewwilllayoutsubviews {     // max height before scroll view go off screen, mess     // table view's constraints , cause sorts of problems     cgfloat maxheight = self.view.size.height                       - self.navigationcontroller.navigationbar.frame.size.height                       - [uiapplication sharedapplication].statusbarframe.size.height;      // height of subviews in scroll view.     cgfloat height = _scrollcontentview.frame.size.height;      if (height > maxheight) {         height = maxheight;     }      // reset scroll view's height appropriate height.     self.scrollviewheightconstraint.constant = height;      [super viewwilllayoutsubviews]; } 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -