需求:一個界面需要加載多個控制器,最底部用UIScrollView來實現(xiàn)滑動,通過自定義頂部的view來實現(xiàn)點擊跳轉(zhuǎn)。
問題:子控制器創(chuàng)建的時候默認帶了xib,如果不在xib里面直接拖東西,控制器的view那么添加到UIScrollView上的寬度就是正確的 ,
但是如果添直接在控制器的xib拖了東西之后,控制器的view添加到UIScrollView上的寬度就會是xib的寬度。
圖片:
如圖,在vc01中的xib中拖了一個UITableView并設(shè)置約束距離左、右、下都為0,距離頂部一定距離。然后將控制器vc01作為子控制器加載到UIScrollView界面的控制器中。
打印結(jié)果:打印出來的這個vc01控制器的view的寬度是vc01 width = 375.000000
附上代碼在主界面的代碼,添加的子控制器就是帶有xib的控制器:
@interface HomeViewController ()<UIScrollViewDelegate>
/** 標簽底部的紅色指示器 */
@property (nonatomic, weak) UIView * indicatorView;
/** 當前選中的按鈕 */
@property (nonatomic, weak) UIButton * selectedButton;
/** 頂部所有標簽 */
@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è)置頂部的標簽欄
*/
- (void)setupTitlesView{
// 標簽欄整體
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; // 可以不寫
[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)部的子標簽
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];
//默認選中第一個按鈕
if (i ==0 ) {
button.enabled = NO;
self.selectedButton = button;
// 讓按鈕內(nèi)部的label根據(jù)文字的內(nèi)容來計算尺寸
[button.titleLabel sizeToFit];
self.indicatorView.width = button.titleLabel.width;
self.indicatorView.centerX = button.centerX;
}
}
[titlesView addSubview:indicatorView];
}
/**
* 按鈕的點擊事件
*/
- (void)titleClick:(UIButton *)button{
// 修改按鈕的狀態(tài)
self.selectedButton.enabled = YES;
button.enabled = NO;
self.selectedButton = button;
// 動畫
[UIView animateWithDuration:0.15 animations:^{
self.indicatorView.width = button.titleLabel.width;
self.indicatorView.centerX = button.centerX;
}];
// 滾動
CGPoint offset = self.contentView.contentOffset;
offset.x = button.tag * self.contentView.width;
[self.contentView setContentOffset:offset animated:YES];
}
/**
* 設(shè)置底部的ScorllView
*/
- (void)setupContentView{
// 不要自動調(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];
// 計算方式就是 寬帶 * 子控制器的個數(shù)
contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
self.contentView = contentView;
// 添加第一個控制器的view
[self scrollViewDidEndScrollingAnimation:contentView];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
// 當前的索引
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(默認為20)
vc.view.height = scrollView.height; // 設(shè)置控制器的view的height的值為整個屏幕的高度(默認是比屏幕高度少20)
[scrollView addSubview:vc.view];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
[self scrollViewDidEndScrollingAnimation:scrollView];
// 點擊按鈕
NSInteger index = scrollView.contentOffset.x / scrollView.width;
[self titleClick:self.titlesView.subviews[index]];
}
求大神指教。萬分感謝。
原因是你并沒有完全指定childViewController的frame,所以它默認是你的xib視圖大小,并不會去適配
在addChildViewController后調(diào)用類似如下方法以適配寬高
#pragma mark- 處理ChildViewController
/// 適配子視圖控制器
- (void)fitFrameForChildViewController:(UIViewController *)childViewController{
//[self.view layoutIfNeeded];
CGRect frame = self.view.frame;
frame.origin.y = 0;
childViewController.view.frame = frame;
}
另外,請盡量不要主動調(diào)用代理方法,滿足相應(yīng)條件它會自己走的
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓(xùn)領(lǐng)域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務(wù)負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。