简体   繁体   English

如何在xcode中从nib文件创建视图?

[英]How to create view from a nib file in xcode?

I have the following code to create a view and put it in scrollview to allow paging 我有以下代码来创建一个视图并将其放在scrollview中以允许分页
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 非常感谢

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). 我知道这是一个旧帖子,但我想创建一个UIView作为一个单独的文件并添加一个xib,所以我可以在我的应用程序的几个地方使用它(几乎就像使用它作为自定义表视图单元格)。 And I couldn't get it quite right, but this post helped me get to the result I wanted. 我无法理解它,但这篇文章帮助我得到了我想要的结果。

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. 然后在界面builder / xib编辑器中,您必须分配为UIView创建的类,以便添加IBOutlets。

Hope it helps someone out, cuz it took me a bit! 希望它可以帮助别人,因为它花了我一点!

I think the thing you're missing is that you can set the frame of your new UIView after loading the nib. 我认为你缺少的是你可以在加载笔尖后设置新UIView的框架。 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. 我也在下面的代码中将加载功能分解成碎片,这样你就可以更容易地看到发生了什么。

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属性。

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更复杂,您可以修改它。

You can use my simple class https://github.com/ulian-onua/KRNNib to create view from nib file with one method call. 您可以使用我的简单类https://github.com/ulian-onua/KRNNib通过一个方法调用从nib文件创建视图。
For example, you can use KRNNib in a next way: 例如,您可以在下一个方式使用KRNNib:

UIView *view = [KRNNib viewFromNibWithName:@"TestView"];
[self.view addSubview:view]; // add instantiated view as subview to view of current UIViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM