Weird positioning issue when tabBar is present

2019-07-18 03:42发布

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.

1条回答
Bombasti
2楼-- · 2019-07-18 04:11

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.

查看更多
登录 后发表回答