使用addChildViewController:方法,通过切换子viewController来切换界面,这个主界面就是一个container viewController的概念。
将子viewController的view加进来的时候,注意修改其frame,让它适应你用来盛装内容的view的大小
另外,childViewController中的所有东西可以通过一个UIScrollView盛装,加过来之后好滑动查看所有内容。
最后的样子:
最后上代码记录一下吧,整个页面:
#import "MonthlyPushViewController.h"
#import "HuiZongPartController.h"
#import "ChengBanPartController.h"
#import "QiTaPartViewController.h"
#import "Masonry.h"
#import "TopButtonView.h"
@interface MonthlyPushViewController ()<TopButtonViewDelegate>
@property (nonatomic, weak) UIViewController *currentSubViewController;
@property (nonatomic, weak) UIView *containerView;
@end
@implementation MonthlyPushViewController
#pragma mark - 生命周期
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"月度推送";
TopButtonView *topButtonView = [[TopButtonView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49) titles:@[@"汇总情况", @"承办情况", @"其他情况"]]; //
topButtonView.delegate = self;
[self.view addSubview:topButtonView];
// 底部放一条线
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
lineView.backgroundColor = COLOR_RGB(243, 243, 243);
[self.view addSubview:lineView];
UIView *container = [[UIView alloc] init];
self.containerView = container;
[self.view addSubview:container];
[container mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(self.view);
make.top.equalTo(lineView.mas_bottom);
}];
// 添加几个子viewController
[self addChildViewController:[[HuiZongPartController alloc] init]];
[self addChildViewController:[[ChengBanPartController alloc] init]];
[self addChildViewController:[[QiTaPartViewController alloc] init]];
// 预先设置初始显示的页面,不要忘记设置frame适应容器view的大小
self.childViewControllers[0].view.frame = self.containerView.bounds;
[self.containerView addSubview:self.childViewControllers[0].view];
self.currentSubViewController = self.childViewControllers[0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - 按钮点击事件,切换底部页面
- (void)topButtonsDidClickedAtIndex:(NSInteger)index {
// 注意,先修改好childViewController的frame
UIViewController *selectedController = self.childViewControllers[index];
selectedController.view.frame = self.containerView.bounds;
if (self.currentSubViewController != self.childViewControllers[index]) {
[self transitionFromViewController:self.currentSubViewController toViewController:self.childViewControllers[index] duration:0.8 options:UIViewAnimationOptionTransitionCurlUp animations:^{} completion:^(BOOL finished) {
if (finished) {
self.currentSubViewController = self.childViewControllers[index];
}
}];
}
}
@end
其他三个子页面分别在对应的自己的ViewController中布局,顶部的三个按钮是自己定义了一个TopButtonView,代码如下:
// TopButtonView.h
#import <UIKit/UIKit.h>
@protocol TopButtonViewDelegate <NSObject>
- (void)topButtonsDidClickedAtIndex:(NSInteger)index;
@end
@interface TopButtonView : UIView
@property (nonatomic, weak) id<TopButtonViewDelegate> delegate;
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titleArray;
@end
// TopButtonView.m
#import "TopButtonView.h"
#import "UtilsMacro.h"
@interface TopButtonView ()
@property (nonatomic, copy) NSMutableArray<UIButton *> *buttonsArray;
@end
@implementation TopButtonView
- (NSMutableArray *)buttonsArray {
if (!_buttonsArray) {
_buttonsArray = [[NSMutableArray alloc] init];
}
return _buttonsArray;
}
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titleArray
{
self = [super initWithFrame:frame];
// 中心线y值
CGFloat centerY = frame.size.height/2 + frame.origin.y;
// 每块视图的宽度,按标题个数评分view的frame宽度
CGFloat eachWidth = frame.size.width / titleArray.count;
if (self) {
for (int i = 0; i < titleArray.count; i ++) {
// 按钮在竖直方向上居中,高30,宽占77%
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * eachWidth + 0.115 * eachWidth, centerY - 15, eachWidth * 0.77, 30)];
button.layer.cornerRadius = 15;
button.layer.masksToBounds = true;
[button setTitle:titleArray[i] forState:UIControlStateNormal];
[button.titleLabel setFont:SYSTEMFONT(14)];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonsArray addObject:button];
[self addSubview:button];
}
[self changeSelectedTabStyle:0];
}
return self;
}
// 点击之后,事件响应
- (void)buttonClicked:(UIButton *)button {
[self.buttonsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj == button) {
if ([self.delegate respondsToSelector:@selector(topButtonsDidClickedAtIndex:)]) {
[self changeSelectedTabStyle:idx];
[self.delegate topButtonsDidClickedAtIndex:idx];
*stop = YES;
}
}
}];
}
// 修改点击之后的样式
- (void)changeSelectedTabStyle:(NSInteger)index {
[self.buttonsArray enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj setBackgroundColor:[UIColor whiteColor]];
[obj setTitleColor:COLOR_RGB(3, 169, 244) forState:UIControlStateNormal];
if (idx == index) {
[obj setBackgroundColor:COLOR_RGB(3, 169, 244)];
[obj setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
}];
}
@end