當 創(chuàng)建一個字控件View ,并且子空間view顯示view 超過自身的view時。可以將子空間的View 添加到父控件
1.創(chuàng)建父控件變量 supView
- 【supView addSubView View】;
在自定義View 時,不要把全部東西都寫在init內(nèi)部。。
特別是代理和與父控件有關系的數(shù)據(jù)傳遞時,要特別注意這一點。。
比如: LeftNavView *leftNav = [[LeftNavView alloc]init];
leftNav.owner = self;
leftNav.supView = self.navigationController.view;
[leftNav defauleSetting];
其中 初始化 leftNavView 時,如果將
import "LeftNavView.m"
-(instancetype)init
{
if (self = [super init]) {
_coverView = [[CoverView alloc]initWithViewCellHeight:ScreenHight num:1];
_coverView.hidden = NO;
[_supView addSubview:_coverView];
self.delegate = self;
self.dataSource = self;
[self CreatSwipe];
[self CreatHeard]
}
return self;
}
這樣寫,在視圖初始化的時候,就會將代碼全部加載進去。造成 leftNav.supView = self.navigationController.view; 這句代碼不會被執(zhí)行,因此。所有與supview相關的代碼也無意義。
應該這樣寫:
在主控制器 : LeftNavView *leftNav = [[LeftNavView alloc]init];
leftNav.owner = self;
leftNav.supView = self.navigationController.view;
[leftNav defauleSetting];
而在次級視圖上聲明一個方法
-(void)defauleSetting;
在這個方法,完成view內(nèi)容的添加。。