![](/img/trans.png)
[英]Design UIView subclass auto layout with custom size in Interface Builder xib file
[英]Custom UIView added in the interface builder does not load the xib
我用xib創建了自定義UIView
。
另外我在我的故事板中有一個UIViewController
,我已經添加了一個UIView
並將其類設置為我的自定義UIView
。
但是當我運行應用程序時,視圖沒有其子視圖。 調試時,所有子視圖都為null。
在自定義UIView
.m中,存在以下init方法:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
我錯過了什么?
如您所知,使用UIViewController
我們使用-initWithNibName:bundle:
方法將其與xib連接。
但...
說到UIView
,你需要使用-loadNibNamed:owner:options:
並用xib加載它。 ( 只是在xib中為視圖指定自定義類將不起作用 )
CustomXIBView
的UIView
子類
UIView
子類 ) CustomXIBView
CustomXIBView
的筆尖 View
( 左側工具欄 ) Show Identity Inspector
( 右側面板中的第3個選項 ) CustomXIBView
指定為View
的自定義類
CustomXIBView
的File's Owner
做任何事情 CustomXIBView.h
連接 //To load `CustomXIBView` from any `UIViewController` or other class:
//instead of the following commented code, do the uncommented code
//CustomXIBView *myCustomXIBViewObj = [CustomXIBView alloc] init];
//[myCustomXIBViewObj setFrame:CGRectMake(0,0,320,480)];
//Do this:
CustomXIBView *myCustomXIBViewObj =
[[[NSBundle mainBundle] loadNibNamed:@"someView"
owner:self
options:nil]
objectAtIndex:0];
[myCustomXIBViewObj setFrame:CGRect(0,
0,
myCustomXIBViewObj.frame.size.width,
myCustomXIBViewObj.frame.size.height)];
[self.view addSubview:myCustomXIBViewObj];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.