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
------------------------------------------------------------------------------------------
相关推荐
Apress.Pro.dot.NET.2.0.Windows.Forms.and.Custom.Controls.in.C.Sharp.Dec.2005.pdf
This concise 150 page book will help you create and use practical code analysis tools utilizing the new features of Microsoft’s Roslyn compiler to understand the health of your code and identify ...
jquery.mobile.custom.min.js v1.4.2 偶然发现这文件不好找还有挺贵,所以传一个
[confluence插件] custom-charts-server-5.2.12.obr [confluence插件] custom-charts-server-5.2.12.obr [confluence插件] custom-charts-server-5.2.12.obr [confluence插件] custom-charts-server-5.2.12.obr ...
This book equips the reader with skills necessary to design and build custom hardware interfaces for the Raspberry Pi. A thorough chapter on interfacing 5-volt systems to 3.3-volt Raspberry Pis ...
c#form和自定义控件开发
A UIKit custom transition that simulates an elastic drag. Written in Swift..zip,模拟弹性阻力的uikit自定义转换。用斯威夫特写的。
Hex.Workshop.Professional.6.8.0.5419.incl.Patch.And.Custom-MPT
javascript 版 生成二维码+全流程演示
You will start by creating your first Android custom view and go through the design considerations. You will then see how the right choices will enable your custom view to perform seamlessly across ...
Game Development and Simulation with Unreal Technology explores the use of Unreal Engine 4 (UE4) for the development of real-time digital interactive contents to be used in computerized games or ...
1.4. What’s new in Spring 1.4.1. What was new in Spring 3.1? 1.4.2. What was new in Spring 3.2? 1.4.3. What’s new in Spring 4.0? 1.5. Summary Chapter 2. Wiring beans 2.1. Exploring Spring’s ...
It shows you how to change and manage the general setting of your store and manage the modules and their layout, along with files and folders of the default theme. You will explore the featured module...
You’ll learn how to build your own custom game engine with reusable components so that you can quickly develop games with maximum impact and minimum code. You’ll also learn the secrets of vector ...
Chapter 3—Editors, IDE’s and Other tools.........................................................................28 3.1 Basic requirements for a decent Go development environment......................
CoCServer-1-v15-U2 (Custom & CoC Mods - Unlimited).apk
Creating Your First Android Project in Eclipse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Examining the Android-Created Files . . . . . . . . . . . . . . . . . . . . . . . . . . ...
It shows you how to change and manage the general setting of your store and manage the modules and their layout, along with files and folders of the default theme. You will explore the featured module...
EO.WebBrowser is a web browser engine based on Google's Chromium project but with native .NET programming interface --- don't worry, it's not a wrapper around the Chrome browser installed on your ...
The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...