iphone - 将 UIView 添加到另一个 UIView 时 y 位置不正确

标签 iphone ios uiview uidatepicker

我使用以下代码向 View 添加了两个 UIViewsself.view 是我在导航 Controller 中的 View 。

CGRect rect = [self.view bounds];
CGRect pickerFrame = CGRectMake(0, self.view.frame.size.height - 
                                       self.datePicker.frame.size.height - 44,
                                self.datePicker.frame.size.width,
                                self.datePicker.frame.size.height);
CGRect tableFrame = CGRectMake(0, 0, self.tView.frame.size.width, 
                               self.view.frame.size.height - 
                                   self.datePicker.frame.size.height);
[self.datePicker setFrame:pickerFrame];
[self.tView setFrame:tableFrame];

[self.view addSubview:self.tView];
[self.view addSubview:self.datePicker];

我预计选择器位于 tableView 下方,但结果并非如此。结果是两个对象之间有一个 44 像素宽的条。是什么原因?我不明白这个问题。

最佳答案

如果要在tableview下方添加picker,那么首先设置tableview的frame,然后相应地设置picker的Y位置。假设这是你的 iPhone 应用程序,你想添加一个表格,然后添加一个 pickerview 那么你的代码将是这样的 -

CGRect tableFrame = CGRectMake(0, 0, self.tView.frame.size.width, (self.view.frame.size.height - self.datePicker.frame.size.height));

[self.tView setFrame:tableFrame];

然后像这样设置pickerview的框架-

CGRect pickerFrame = CGRectMake(0, self.tView.frame.size.height+2, self.datePicker.frame.size.width, self.datePicker.frame.size.height);
[self.datePicker setFrame:pickerFrame];

然后将这些对象添加到您的 View 中

[self.view addSubview:self.tView];
[self.view addSubview:self.datePicker];

谢谢

关于iphone - 将 UIView 添加到另一个 UIView 时 y 位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805792/

相关文章:

ios - 如何设置不扭曲设备旋转的旋转变换?

ios - 在 iPad 上使用 UIActionSheet 时调暗并锁定背景

iphone - 向 Apple Store 提交 iPhone 应用程序并定价

iphone - iOS动态行宽

ios - 多点连接自动静默连接

iphone - 如何将 UIView 渲染到 CGContext

iphone - 如何从UICollectionView删除项目?

iphone - 在 iPhone 应用程序更新上将内容更新/插入到现有 Sqlite 数据库中

iphone - iOS 5 : Is it possible to write a struct to a plist file?

ios - 无法将 UIButton 事件正确发送到其他 View Controller