7064

Weird positioning issue when tabBar is present

We have a custom view with a UIPickerView plus toolbar (216 + 44). At init time (viewDidLoad) this custom view is pushed below the screen using the following piece of code.

CGPoint newOrigin; newOrigin.x = pickerViewOutlet.frame.size.width/2; newOrigin.y = self.view.frame.size.height + ((pickerViewOutlet.frame.size.height)/2); NSLog(@"%f,%f",self.view.frame.size.height,(pickerViewOutlet.frame.size.height)/2); pickerViewOutlet.center = CGPointMake(newOrigin.x, newOrigin.y);

When a button is clicked this view is pulled up using the following piece of code.

[self.view bringSubviewToFront:pickerViewOutlet]; NSLog(@"tabbar %f",self.tabBarController.tabBar.frame.size.height); CGPoint showOrigin; showOrigin.x = pickerViewOutlet.frame.size.width/2; showOrigin.y = pickerViewOutlet.center.y - pickerViewOutlet.frame.size.height; //self.tabBarController.tabBar.frame.size.height ; NSLog(@"showpicker %f,%f",pickerViewOutlet.center.y,pickerViewOutlet.frame.size.height); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.5]; pickerViewOutlet.center=CGPointMake(showOrigin.x, showOrigin.y); [UIView commitAnimations]; [pickerCtrlOutlet reloadAllComponents];

This works fine. However this does not work (part of the view is below the tab bar) in the presence of a bottom tab bar controller on the page even though the code is modified as

showOrigin.y = pickerViewOutlet.center.y - pickerViewOutlet.frame.size.height - self.tabBarController.tabBar.frame.size.height ;

However if the above code is modified to

showOrigin.y = pickerViewOutlet.center.y - pickerViewOutlet.frame.size.height - self.tabBarController.tabBar.frame.size.height - 90;

it works perfectly where the view is right above the tab bar.

Answer1:

As far as I know, in viewDidLoad the self.view is not yet added to the superview and thus the frame is not set to the correct sizes.

For example, you can design a UIView in InterfaceBuilder and it will have 320x460. When you add it to the superview it will actually become smaller because of the bottom tab bar. The auto-resizing mechanism helps in this matter.

So, I think you are positioning the picker view using the wrong values in viewDidLoad and then when you use a new position relative to its old one, it will still be wrong.

Here is how I would write this:

[self.view bringSubviewToFront:pickerViewOutlet]; NSLog(@"tabbar %f",self.tabBarController.tabBar.frame.size.height); CGPoint showOrigin; showOrigin.x = pickerViewOutlet.frame.size.width/2; //Notice this line ----------- showOrigin.y = self.view.frame.size.height - pickerViewOutlet.frame.size.height / 2; //self.tabBarController.tabBar.frame.size.height ; NSLog(@"showpicker %f,%f",pickerViewOutlet.center.y,pickerViewOutlet.frame.size.height); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.5]; pickerViewOutlet.center=CGPointMake(showOrigin.x, showOrigin.y); [UIView commitAnimations]; [pickerCtrlOutlet reloadAllComponents];

Notice showOrigin.y = self.view.frame.size.height - ...

And (for extra points :) you can set the autoresizing masks for the picker view to Flexible Top (or, lock the bottom coordinate). If you do this, even if you position the picker view in viewDidLoad and then the self.view resizes, the pickerview will also change it's position.

Recommend

  • Please explain me about this leak
  • UITabBar wont hide
  • UIView isn't released. Calling delegate with self, and using UIView commitAnimations adds retai
  • AnimationDidStop not being called
  • How to set the background color of navigation tab in android
  • Change the Height of an ExtJS 4 Tab
  • What is the purpose of multidimensional arrays? [closed]
  • Access NSArray from a NSTimer Interval = EXC_BAD_ACCESS
  • Is there a way to make a vertical?
  • Fetch/Refresh Health Kit Data Programmatically?
  • UITableViewCell change selectedBackgroundView alpha?
  • Escape special characters in MySQL using C# and ASP.Net
  • What's the usecase for ons.ready()?
  • AngularJS DOM markup
  • this class is not key value coding-compliant for the key PerformRotate
  • detecting end of http header with \\r\\n\\r\\n
  • Change paused animation's play state to running on click of a link
  • Custom font for MKAnnotationView Callout
  • jQuery, Calling multiple animations in a row on click
  • Force iPad app to launch in portrait when landscape is supported
  • Samsung Galaxy Tab 10.1 and -webkit-tap-highlight-color style?
  • Opening links in a new tab and only the new tab
  • Cannot run the macro
  • send data back from jsp iterator to struts action class
  • How to get the click event on sIFR embedded tag in IE?
  • qt how to know that a pushbutton is clicked?
  • Can I use worksheet_change for a specific column only?
  • Best practice for switching iPhone views?
  • Winforms: Add a close “x” button in a UserControl
  • Entity Framework ObjectContext: Concurrency
  • change color of jstree node
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Selenium to click on a javascript button corresponding to a text
  • How to remove a SwiftyJSON element?
  • Detect when Facebook like button is clicked
  • Row Count Is Returning the incorrect number using RaptureXML
  • Adding a button at the bottom of a table view
  • Obtain ObjectIdHex value from mgo query
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Bitwise OR returns boolean when one of operands is nil