ios - 使用自动布局支持以编程方式更改 UITableView 位置

标签 ios uitableview storyboard autolayout

我想以编程方式更改 UITableview 的位置。我在 viewDidLoad,ViewWillAppear 回调中添加了以下代码,但位置没有改变。

Table.frame = CGRectMake(0,100,320,200);

当我在 Storyboard 中禁用 Auto Layout 选项时,Table View 的位置会发生变化。但是,我需要保留 Auto Layout 选项以保持对不同设备类型的支持。

最佳答案

使用自动布局,并对 tableView 的位置和大小设置约束。 然后,创建 NSLayoutConstraint 类型的 IBOutlet 实例,并将它们附加到 xib 中的约束。

然后,在您的代码中,设置约束值以重置 TableView 位置。

示例代码:

在你的 .h 文件中:

@property IBOutlet NSLayoutConstraint *tableOriginX;
@property IBOutlet NSLayoutConstraint *tableOriginY;
@property IBOutlet NSLayoutConstraint *tableWidth;
@property IBOutlet NSLayoutConstraint *tableHeight;

然后,在您要更改表格框架的 .m 文件中:

tableOriginX.constant = 0;
tableOriginY.constant = 100;
tableWidth.constant   = 320;
tableHeight.constant  = 200;

关于ios - 使用自动布局支持以编程方式更改 UITableView 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305728/

相关文章:

ios - 如何开发屏幕之间的滑动

ios - "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update"尝试插入行时

ios - 控制可能到达结束非空函数

c# - 当受影响的控件被隐藏时,WPF Storyboard动画是否继续运行?

ios - 如何获取 CIFilter 的显示名称?

ios - UserDefault 保存按钮状态

iphone - 我是否需要昂贵的企业开发者帐户才能使用 TestFlightApp 进行 Beta 测试?

ios - 检测未使用的方法 iOS

ios - Xcode 9.2 - “Fixed Width Constraints May Cause Clipping” 警告

ios - 将某些 XIB/Storyboard 字符串标记为不可本地化