iOS 在同一个页面中,通过childViewController切换界面

本文介绍了如何在iOS应用中通过addChildViewController方法实现页面切换,利用container viewController概念,将子viewController的view添加并调整frame以适应内容区域。同时,通过UIScrollView展示所有子页面内容,提供滑动查看功能。代码示例展示了整体页面结构,而子页面布局和顶部按钮控件(TopButtonView)在各自 ViewController 中完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值