0

我已经在纸上计算了位置和偏移量。我已经编写了代码并调试了我得到预期的结果。然而,在 iPhone 模拟器上,事物重叠了大约 15 个像素。

要继续调试,我需要知道 UI 对象在屏幕上的确切位置。

与弹出搜索键盘和在静态 UISearchBar 和动态添加的 UITabBar 之间调整 UITableView 的大小相关(表格视图嵌入到选项卡之一中)。不,由于旋转和不同的屏幕尺寸,我真的不想使用任何硬编码值。

我怎样才能知道这个?

- (void)keyboardWasShown:(NSNotification*)aNotification
{
if (self.keyboardShown)
    return;

// Get keyboard dimensions
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize kbSize = [aValue CGRectValue].size;
NSValue* endValue = [info objectForKey:UIKeyboardCenterEndUserInfoKey];
CGPoint endCenter = [endValue CGPointValue];

CGRect frame = myView.frame;
frame.size.height = endCenter.y - kbSize.height/2 - frame.origin.y;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2];

myView.frame = frame;

[UIView commitAnimations];

self.keyboardShown = YES;
}

...代码以防万一我再也看不到明显的错误...

4

1 回答 1

1

你最好的选择可能是 UIView 的convertPoint:toView:. 要找出给定视图的位置,请使用:

CGPoint myPoint = [myView convertPoint:CGPointMake(0, 0) toView:((YourAppDelegate *)[[UIApplication sharedApplication] delegate]).window];
于 2010-01-27T20:45:27.253 回答