gpt4 book ai didi

objective-c - UIViewController viewDidLoad

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:28 25 4
gpt4 key购买 nike

我有一个新项目,我可以使用 iOS 5+ 功能,所以我选择同时使用 AutoLayout 和 UIViewController childControllers 功能。

我尝试创建的屏幕很简单: 1.顶部区域有很多“复杂”的控件。 2. 底部区域是可视化结果的tableview。

我想要什么

我希望控件区域占据所有必要的位置(即我给 Interface Builder 的位置)。 TableView 将根据需要缩小。

问题

从 XIB 加载时,UIViewController.view 框架的大小始终为 0x568。我应该是 0x200(如果 200 是我在 Interface Builder 中配置的大小)。

有效方法:使用 AutoLayout

我用我所有的元素创建了一个 XIB。我在“ Root View ”上做了 socket ,我做了 socket 和所有 View 容器。

然后在我的 viewDidLoad 方法中添加容器并配置约束。

-(void) setupConstraints
{
NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:1];

// Views dictionary.
UIView *topView = self.topView;
UIView *table = self.tableTracks;
NSDictionary *views = NSDictionaryOfVariableBindings(topView, table);

// Views metrics.
NSNumber *topViewHeight = [NSNumber numberWithFloat:self.topView.frame.size.height];
NSDictionary *metrics = NSDictionaryOfVariableBindings(topViewHeight);

// Pin the topView to the top edge of the container.
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(==topViewHeight)][table]|" options:0 metrics:metrics views:views]];

// Pin the topView edges to both sides of the container.
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];

// Pin the table edges to both sides of the container.
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:nil views:views]];

[self.view addConstraints:constraints];
}

有了这个,我可以使用 Interface Builder 简单地调整 topView View 的大小,并且 TableView 将根据需要调整大小(没有压缩阻力,我不关心我们是否看不到表格)。

什么不起作用:AutoLayoutChildControllers

为了简单起见,我选择使用 ChildControllers(执行自定义 UIFont 设置需要很多 socket ,可惜 XCode 还不能处理它们!)。我做了以下修改:

  1. 创建了 HomeTopViewController 类 + XIB。创建 HomeTableViewController 类 + XIB。
  2. 将源中的所有 View 复制到正确的 XIB。添加 UIViewController 引用 + 连接 socket 。
  3. HomeTopViewController 的根容器配置为 200px 高度。
  4. 将我的容器连接到我 child Controller 的 View 导出。

然后我将设置代码更新为以下内容:

-(void) _addChildViewControllersAndSetupConstraints {
// Get required metrics variables
NSNumber *topViewHeight = [NSNumber numberWithFloat:self.topViewController.view.frame.size.height];

CFShow(self.topViewController.view);
// log is : <UIView: 0xa209c20; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0xa2097e0>>
NSLog(@"self.topViewController.view.frame.size.height = %f", self.topViewController.view.frame.size.height);


// Informs controllers the ADD operation started
[self addChildViewController:self.topViewController];
[self addChildViewController:self.tableViewController];

// Add view to the view's hierarchy
self.topViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
self.tableViewController.view.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:self.topViewController.view];
[self.view addSubview:self.tableViewController.view];


// Setup constraints
NSMutableArray *constraints = [NSMutableArray arrayWithCapacity:1];

// Views dictionary
UIView *topView = self.topViewController.view;
UIView *table = self.tableViewController.view;

NSDictionary *views = NSDictionaryOfVariableBindings(topView, table);

// Views metrics dictionary
NSDictionary *metrics = NSDictionaryOfVariableBindings(topViewHeight);

// Pin the topView to the top edge of the container.
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topView(==topViewHeight)][table]|" options:0 metrics:metrics views:views]];

// Pin the topView edges to both sides of the container.
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[topView]|" options:0 metrics:nil views:views]];

// Pin the table edges to both sides of the container.
[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:nil views:views]];

// Adds constraints
[self.view addConstraints:constraints];


// Informs controllers the ADD operation ended
[self.topViewController didMoveToParentViewController:self];
[self.tableViewController didMoveToParentViewController:self];
}

问题

无论我如何调整 HomeTopViewController 中 UIView 容器的大小,CFShow(self.topViewController.view); 行总是给我 frame = (0 0 ; 320 568) 当我期望 200px 时。

我确实将布局大小配置为“自由格式”或“无”。

我想尽量避免的事情

我不想创建其他 socket ,然后在 HomeTopViewControllerHomeTableViewController 上创建 View 并将初始帧/高度存储到 UIViewController 属性中。

问题

  1. 在加载时,是否所有 controller.view.frame 属性都在屏幕上调整了大小? (无论是 3.5 还是 4 英寸)
  2. 如何在不在某处硬编码大小的情况下解决这个问题//创建额外的导出?

最佳答案

如果您使用 Storyboard,这会更容易。您可以将容器 View 添加到主 Controller 的 View 中,并根据需要调整它们的大小,并且您会自动将 View Controller 嵌入到这些设置为正确大小的容器 View 中。这些容器 View (在对象列表中的常规 UIView 旁边)只能从 Storyboard 中使用,而不是 xibs。这几乎不需要代码,而且您可能不必手动添加任何约束。

关于objective-c - UIViewController viewDidLoad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14144033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com