如何在xcode中从nib文件创建视图? [英] How to create view from a nib file in xcode?

查看:94
本文介绍了如何在xcode中从nib文件创建视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来创建一个视图并将其放在scrollview中以允许分页

代码工作正常然而我不能做的是从nib文件加载视图

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

换句话说
我想使用initWithNibName而不是initWithFrame?

in other words I want to use "initWithNibName" instead of "initWithFrame"?

   - (void)createPageWithColor:(UIColor *)color forPage:(int)page
     {
     UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 300,400)];
         newView.backgroundColor = color;
     [scrollView addSubview:newView];
    }

非常感谢

推荐答案

我认为你缺少的是你可以在加载笔尖后设置新UIView的框架。加载/初始化时间不是你唯一的注意力。我也在下面的代码中将加载函数分解成碎片,这样你就可以更容易地看到发生了什么。

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.

观察:

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];

不要忘记为了让UIScrollView实际滚动,你需要将其contentSize属性设置为其中的商品大小,可能大于滚动视图本身的.frame属性。

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.

这篇关于如何在xcode中从nib文件创建视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆