需求:一個(gè)界面需要加載多個(gè)控制器,最底部用UIScrollView來(lái)實(shí)現(xiàn)滑動(dòng),通過(guò)自定義頂部的view來(lái)實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)。
問(wèn)題:子控制器創(chuàng)建的時(shí)候默認(rèn)帶了xib,如果不在xib里面直接拖東西,控制器的view那么添加到UIScrollView上的寬度就是正確的 ,
但是如果添直接在控制器的xib拖了東西之后,控制器的view添加到UIScrollView上的寬度就會(huì)是xib的寬度。
圖片:
如圖,在vc01中的xib中拖了一個(gè)UITableView并設(shè)置約束距離左、右、下都為0,距離頂部一定距離。然后將控制器vc01作為子控制器加載到UIScrollView界面的控制器中。
打印結(jié)果:打印出來(lái)的這個(gè)vc01控制器的view的寬度是vc01 width = 375.000000
附上代碼在主界面的代碼,添加的子控制器就是帶有xib的控制器:
@interface HomeViewController ()<UIScrollViewDelegate>
/** 標(biāo)簽底部的紅色指示器 */
@property (nonatomic, weak) UIView * indicatorView;
/** 當(dāng)前選中的按鈕 */
@property (nonatomic, weak) UIButton * selectedButton;
/** 頂部所有標(biāo)簽 */
@property (nonatomic, weak) UIView * titlesView;
/** 底部的所有內(nèi)容 */
@property (nonatomic, weak) UIScrollView * contentView;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
[self setupChildVCs];
[self setupTitlesView];
[self setupContentView];
}
- (void)viewDidLayoutSubviews{
// 2. 在這里拿到的寬度是正確的
NSLog(@"layo_self.view.frame.size.width = %f", self.view.bounds.size.width);
}
/**
* 初始化子控制器
*/
- (void)setupChildVCs{
Test01ViewController * vc01 = [[Test01ViewController alloc]init];
vc01.title = @"vc01";
vc01.view.backgroundColor = [UIColor redColor];
[self addChildViewController:vc01];
NSLog(@"vc01 width = %f", vc01.view.bounds.size.width);
Test02ViewController * vc02 = [[Test02ViewController alloc]init];
vc02.title = @"vc02";
vc02.view.backgroundColor = [UIColor yellowColor];
[self addChildViewController:vc02];
NSLog(@"vc02 width = %f", vc02.view.bounds.size.width);
}
/**
*設(shè)置頂部的標(biāo)簽欄
*/
- (void)setupTitlesView{
// 標(biāo)簽欄整體
UIView * titlesView = [[UIView alloc]init];
// titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
titlesView.backgroundColor = [UIColor blueColor];
// titlesView.width = self.view.width; // 寬度
titlesView.width = [UIScreen mainScreen].bounds.size.width;
titlesView.height = TitlesViewH;
titlesView.y = TitlesViewY;
titlesView.x = 0; // 可以不寫(xiě)
[self.view addSubview:titlesView];
self.titlesView = titlesView;
// 底部的紅色指示器
UIView * indicatorView = [[UIView alloc]init];
indicatorView.backgroundColor = [UIColor redColor];
indicatorView.height = 2;
indicatorView.tag = -1;
indicatorView.y = titlesView.height - indicatorView.height;
self.indicatorView = indicatorView;
// 內(nèi)部的子標(biāo)簽
CGFloat width = titlesView.width / self.childViewControllers.count;
NSLog(@"width = %f, all width = %f", width, self.view.width);
CGFloat height = titlesView.height;
for (NSInteger i = 0; i < self.childViewControllers.count; i++) {
UIButton * button = [[UIButton alloc]init];
button.tag = i;
button.height = height;
button.width = width;
button.x = i * width;
UIViewController * vc = self.childViewControllers[i];
[button setTitle:vc.title forState:UIControlStateNormal];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
[titlesView addSubview:button];
//默認(rèn)選中第一個(gè)按鈕
if (i ==0 ) {
button.enabled = NO;
self.selectedButton = button;
// 讓按鈕內(nèi)部的label根據(jù)文字的內(nèi)容來(lái)計(jì)算尺寸
[button.titleLabel sizeToFit];
self.indicatorView.width = button.titleLabel.width;
self.indicatorView.centerX = button.centerX;
}
}
[titlesView addSubview:indicatorView];
}
/**
* 按鈕的點(diǎn)擊事件
*/
- (void)titleClick:(UIButton *)button{
// 修改按鈕的狀態(tài)
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 動(dòng)畫(huà)
[UIView animateWithDuration:0.15 animations:^{
self.indicatorView.width = button.titleLabel.width;
self.indicatorView.centerX = button.centerX;
}];
// 滾動(dòng)
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
/**
* 設(shè)置底部的ScorllView
*/
- (void)setupContentView{
// 不要自動(dòng)調(diào)整inset
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView * contentView = [[UIScrollView alloc]init];
contentView.frame = self.view.frame;
contentView.delegate = self;
contentView.pagingEnabled = YES;
[self.view insertSubview:contentView atIndex:0];
// 計(jì)算方式就是 寬帶 * 子控制器的個(gè)數(shù)
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;
// 添加第一個(gè)控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
// 當(dāng)前的索引
NSInteger index = scrollView.contentOffset.x / scrollView.width;
// 取出子控制器
UIViewController * vc = self.childViewControllers[index];
vc.view.x = scrollView.contentOffset.x;
vc.view.y = 0; // 設(shè)置控制器的view的y值為0(默認(rèn)為20)
vc.view.height = scrollView.height; // 設(shè)置控制器的view的height的值為整個(gè)屏幕的高度(默認(rèn)是比屏幕高度少20)
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 點(diǎn)擊按鈕
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleClick:self.titlesView.subviews[index]];
}
求大神指教。萬(wàn)分感謝。
原因是你并沒(méi)有完全指定childViewController
的frame,所以它默認(rèn)是你的xib視圖大小,并不會(huì)去適配
在addChildViewController
后調(diào)用類(lèi)似如下方法以適配寬高
#pragma mark- 處理ChildViewController
/// 適配子視圖控制器
- (void)fitFrameForChildViewController:(UIViewController *)childViewController{
//[self.view layoutIfNeeded];
CGRect frame = self.view.frame;
frame.origin.y = 0;
childViewController.view.frame = frame;
}
另外,請(qǐng)盡量不要主動(dòng)調(diào)用代理方法,滿足相應(yīng)條件它會(huì)自己走的
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。