`
woainike
  • 浏览: 79799 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

Custom your view and load in code. It's difference viewcontrolller.

 
阅读更多

 

You need to load it using the -loadNibNamed method. -initWithNibName is only for UIViewControllers.

Add the following code to your MyCustomView init method:

 

 

NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
[self addSubview:mainView];

 Remember, if you are initializing an object from a nib, it calls - (id)initWithCoder:(NSCoder *)aDecoder to initialize, so you'll have to override that if you are creating the MyCustomView object within the nib. If you're just doing it with initWithFrame:, then just override that and add the code above. Also, in your nib, make sure you have one top-level UIView, and place all other elements within that (that makes sure that your subviewArray only has one entry).

 

This will load the views from the nib and add them to the object, and should do the trick.


Conversation

 

 

------------------------------------------------------------------------------------------
I used your code in the initWithFrame and now I can see the MyCustomView with all its elements, but there is one thing that doesn't work. In the viewDidLoad of MyViewController I've: UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame]; [subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]]; [myTableView addSubview:subView]; [subView release]; so that I should see the table under it. Any ideas?
 – Cricket

------------------------------------------------------------------------------------------

It works, if I set it in the initWithFrame. Now I've to understand how support the landscape mode for this view :) Thanks 

------------------------------------------------------------------------------------------

I've found another solution that resolves also the landscape problem. I use the loadNibNamed code in MyViewController (when I have to display the subview) and I put any other customization in the awakeFromNib method in MyCustomView.m –

------------------------------------------------------------------------------------------

Hey, the method [self addSubview:mainView] only adds the view you made in IB to the MyCustomView. I tried your method and then tried to set the frame for MyCustomView. Turns out that the frame changes but the frame of the view made in .xib does not change. How do i change the frame of the IB view through MyCustomView? – HG's 

------------------------------------------------------------------------------------------

Just do mainView.frame = newFrame. So, if you want the IB view's frame to take up the entire MyCustomView frame, do "mainView.frame = self.bounds". – Ned 

------------------------------------------------------------------------------------------

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics