存在tabBar时出现奇怪的定位问题 [英] Weird positioning issue when tabBar is present

查看:59
本文介绍了存在tabBar时出现奇怪的定位问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带有UIPickerView加上工具栏(216 + 44)的自定义视图.在初始化时(viewDidLoad),使用以下代码将该自定义视图推送到屏幕下方.

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.

推荐答案

据我所知,在viewDidLoad中, self.view 尚未添加到超级视图中,因此未设置框架到正确的尺寸.

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.

例如,您可以在InterfaceBuilder中设计一个UIView,它将具有320x460.当您将其添加到超级视图时,由于底部的标签栏,它实际上会变小.自动调整大小机制可以帮助解决此问题.

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.

因此,我认为您在viewDidLoad中使用错误的值来定位选择器视图,然后相对于旧位置使用新位置时,它仍然是错误的.

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.

这是我的写法:

   [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];

通知 showOrigin.y = self.view.frame.size.height-...

然后(为了加分:),您可以将选取器视图的自动调整大小蒙版设置为柔性顶部"(或锁定底部坐标).如果这样做,即使将选择器视图放置在viewDidLoad中,然后调整self.view的大小,pickerview也将更改其位置.

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.

这篇关于存在tabBar时出现奇怪的定位问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆