如何从 xcode 中的 nib 文件创建视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3089113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create view from a nib file in xcode?
提问by ahmed
I have the following code to create a view and put it in scrollview to allow paging
the code works fine however what I couldn't do is loading views from a nib file
我有以下代码来创建一个视图并将其放在滚动视图中以允许分页
代码工作正常但是我不能做的是从 nib 文件加载视图
in other words I want to use "initWithNibName" instead of "initWithFrame"?
换句话说,我想使用“initWithNibName”而不是“initWithFrame”?
- (void)createPageWithColor:(UIColor *)color forPage:(int)page
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 300,400)];
newView.backgroundColor = color;
[scrollView addSubview:newView];
}
Thanks alot
非常感谢
回答by Matt W.
I know this is an old post, but I wanted to create a UIView as a separate file and add a xib so I could use it in several places in my app (almost like using it as a custom table view cell). And I couldn't get it quite right, but this post helped me get to the result I wanted.
我知道这是一篇旧帖子,但我想创建一个 UIView 作为单独的文件并添加一个 xib 以便我可以在我的应用程序的多个位置使用它(几乎就像将它用作自定义表格视图单元格一样)。我不能完全正确,但这篇文章帮助我得到了我想要的结果。
So just if anyone wants to do it the same way, this is what I did:
所以如果有人想以同样的方式做,这就是我所做的:
Add this to the initialization code in your UIView's .m file:
将此添加到 UIView 的 .m 文件中的初始化代码中:
- (id)initWithFrame:(CGRect)frame
{
self = [NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil][0];
if (self)
{
self.frame = frame;
}
return self;
}
Then in interface builder/xib editor you have to assign the class you created for the UIView so you can add IBOutlets.
然后在界面构建器/xib 编辑器中,您必须分配您为 UIView 创建的类,以便您可以添加 IBOutlets。
Hope it helps someone out, cuz it took me a bit!
希望它可以帮助某人,因为它花了我一点时间!
回答by Dan Ray
I think the thing you're missing is that you can set the frame of your new UIView after loading the nib. Load/Init time isn't your only shot at that. I'm also breaking the load function into its pieces, in the code below, so you can see more easily what's going on.
我认为您缺少的是您可以在加载笔尖后设置新 UIView 的框架。加载/初始化时间不是您唯一的尝试。我还在下面的代码中将 load 函数分解为多个部分,以便您可以更轻松地看到发生了什么。
Observe:
观察:
NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:@"yournib"
owner:self
options:nil];
//I'm assuming here that your nib's top level contains only the view
//you want, so it's the only item in the array.
UIView *myView = [nibContents objectAtIndex:0];
myView.frame = CGRectMake(0,0,300,400); //or whatever coordinates you need
[scrollview addSubview:myView];
Don't forget that for that UIScrollView to actually scroll, you need to set its contentSize property to the size of the goods inside it, which is likely bigger than the .frame property of the scroll view itself.
不要忘记,要使 UIScrollView 真正滚动,您需要将其 contentSize 属性设置为其中的商品大小,这可能比滚动视图本身的 .frame 属性大。
回答by Cheddar
Try something like this (adapted from "The iPhone Developers Cookbook", pg. 174):
尝试这样的事情(改编自“iPhone 开发者手册”,第 174 页):
UIView *newView = [[[NSBundle mainBundle] loadNibNamed:@"yournib" owner:self options:nil] lastObject];
This assumes a single view object in your .xib, but you could modify it if your .xib is more complicated.
这假设您的 .xib 中有一个视图对象,但如果您的 .xib 更复杂,您可以修改它。
回答by Julian D.
You can use my simple class https://github.com/ulian-onua/KRNNibto create view from nib file with one method call.
For example, you can use KRNNib in a next way:
您可以使用我的简单类https://github.com/ulian-onua/KRNNib通过一个方法调用从 nib 文件创建视图。
例如,您可以通过以下方式使用 KRNNib:
UIView *view = [KRNNib viewFromNibWithName:@"TestView"];
[self.view addSubview:view]; // add instantiated view as subview to view of current UIViewController