xib 指定的位置在 UIView 加载时移动?
我有几个视图控制器,我希望它们共享一个标题状态栏。此标题视图显示我的应用程序中某些事物的状态。我使用 IB 在其自己的 xib 中布局标头视图的内容,然后还使用 IB 布局视图控制器,在视图控制器的 xib 中添加占位符 UIView 元素,以便当我以编程方式加载标头时。
现在,当我从视图 xib 的内容创建 UIView 子类,并将其作为子视图添加到任何视图控制器的视图中时,我在视图控制器 xib 中为标题视图指定的位置似乎会被忽略,并且标题视图无论如何都会放置在 (0,0) 处。标题视图的所有内容都会加载并显示良好,只是视图控制器中标题视图的位置错误。
以下是我如何加载标题视图并将其添加到视图控制器:
header = [InfoHeader loadFromNib:MY_NIB_NAME withOwner:self];
[self.view addSubview:header];
确认视图控制器 xib 有一个连接到“标题”变量的插座。这是“loadFromNib”方法:
+(InfoHeader *) loadFromNib: (NSString *) nibName withOwner:(id) objectOwner
{
NSBundle *b = [NSBundle mainBundle];
NSArray *nib = [b loadNibNamed:nibName owner:objectOwner options:nil];
InfoHeader *header;
for (id obj in nib)
{
if ([obj isKindOfClass:[InfoHeader class]])
{
header = (InfoHeader *) obj;
}
}
return header;
}
关于这里要检查的内容有什么想法吗?我不想以编程方式定位标题栏,而是让 IB 来完成。
I have a couple of view controllers that I want all to share a header status bar. This header view shows status of certain things in my app. I used IB to lay out the contents of the header view in its own xib, then also used IB to layout the view controllers, adding a placeholder UIView element in the view controller's xibs for when I load in the header programmatically.
Now, when I create a UIView subclass from the contents of the view xib, and add that as a subview to the view of any of my view controllers, the location that I specified for the header view in my view controller xib seems to get ignored, and the header view gets placed at (0,0) no matter what. All the contents of the header view load and appear fine, it's just the location of the header view in the view controller that is wrong.
Here's how I'm loading and adding my header view to my view controller:
header = [InfoHeader loadFromNib:MY_NIB_NAME withOwner:self];
[self.view addSubview:header];
The view controller xib is confirmed to have an outlet connected to the 'header' variable. And here's that 'loadFromNib' method:
+(InfoHeader *) loadFromNib: (NSString *) nibName withOwner:(id) objectOwner
{
NSBundle *b = [NSBundle mainBundle];
NSArray *nib = [b loadNibNamed:nibName owner:objectOwner options:nil];
InfoHeader *header;
for (id obj in nib)
{
if ([obj isKindOfClass:[InfoHeader class]])
{
header = (InfoHeader *) obj;
}
}
return header;
}
Any ideas on what to check here? I'd rather not locate the header bar programmatically, but let IB do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否更改了 IB 中子视图的位置?通常,视图的默认位置 (0, 0) 将是屏幕中大视图的(上、左)坐标。我认为检查一下效果很好
Did you change the location of your subview in IB? Usually, the default location (0, 0) of the view will be the (top, left) coordinate of the big view in the screen. I think checking that will work fine
您可以通过以编程方式指定子视图的框架来完全不使用“占位符”视图来完成此操作:(
该代码假设您已从类中删除了
header
出口,因此它可以是而是局部变量)。或者,如果您更愿意使用占位符(这样您就可以使用 IB 而不是以编程方式指定标头的位置),请按如下所示操作:
You can do this without using a "placeholder" view at all, by programmatically specifying the frame of the subview:
(that code asssumes that you have removed the
header
outlet from your class, so it can be a local variable instead).Or, if you'd rather use the placeholder (so you can specify the header's position using IB instead of programmatically), do it like this: