三级控制器

作者: 952625a28d0d | 来源:发表于2016-03-17 12:00 被阅读53次
  • UITabBarController
  • UINavigationController
  • 一个导航控制器对应一个视图控制器
  • 标签控制器是管理几个固定的控制器,子控制器是并列的,每一个分栏控制器只有一个UITabBar视图,用于显示UITabBatItem实例,UITabBarItem由当前的试图控制器来管理,这一点与导航控制器中的UIBarButtonitem相同。
  • UITabBarController用数组来管理视图控制器,而导航控制器所管理的试图控制器是上下层级的关系
Paste_Image.png
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}

#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    RootViewController *rootVC = [[RootViewController alloc] init];
    // 权限最高的给跟视图控制器,即标签控制器来控制导航控制器,导航控制器来控制视图控制器
    self.window.rootViewController = rootVC;
    return YES;
}```

###root h

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}

import <UIKit/UIKit.h>

@interface RootViewController : UITabBarController

@property(nonatomic, strong) UIView * tabBarView;

  • (void)showTabBar:(BOOL)show;

@end```

root m

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'Heiti SC Light'; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #31595d}p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; min-height: 21.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s10 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s11 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s12 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s13 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s14 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}

#import "RootViewController.h"

// 引入子控制器
#import "ProfieViewController.h"
#import "ColaViewController.h"
#import "MessageViewController.h"
#import "UserViewController.h"
#import "MoreViewController.h"

#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height

CGFloat const tabBarHeight = 49;
CGFloat const buttonWidth = 64;
CGFloat const buttonHeight = 45;

@interface RootViewController ()

@property (nonatomic, strong) UIImageView *selectImageView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 隐藏系统默认样式
    self.tabBar.hidden = YES;
    
    [self initViewController];
    
    [self initTabBarView];
}

#pragma mark - 初始化控制器
- (void)initViewController{
    
    ProfieViewController *p = [[ProfieViewController alloc] init];
    ColaViewController *c = [[ColaViewController alloc] init];
    UserViewController *u = [[UserViewController alloc] init];
    MessageViewController *m = [[MessageViewController alloc] init];
    MoreViewController *more = [[MoreViewController alloc] init];
    NSArray *vcArray = @[p,c,u,m,more];
    NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];
    
    // for循环创建导航控制器
    for (int i = 0; i < vcArray.count; i++) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vcArray[i]];
        [tabArray addObject:nav];
    }
    // 将导航控制器给标签控制器
    self.viewControllers = tabArray;
}

#pragma mark - 自定义标签栏
- (void)initTabBarView{
    // 初始化标签工具栏视图
    self.tabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, screenHeight - tabBarHeight, screenWidth, tabBarHeight)];
    // 设置TabBarView的背景颜色 用图片背景来设置
    _tabBarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mask_navbar"]];
    // 创建一个数组 用来放置TabBarView图片按钮
    [self.view addSubview:_tabBarView];
    // 新语法创建数组
    NSArray *imageArray = @[@"home_tab_icon_1",@"home_tab_icon_2",@"home_tab_icon_3",@"home_tab_icon_4",@"home_tab_icon_5"];
    for (int i = 0; i < imageArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        // button frame的设置  这里注意一下 (tabBarHeight - buttonHeight)/2 的作用是为了让Button居中 要牢记这个方法
        button.frame = CGRectMake(buttonWidth * i, (tabBarHeight - buttonHeight) / 2, buttonWidth, buttonHeight);
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        // 设置tag 值 记得tag值要设置100以后的 因为在iOS中100以前的数值是有别的用处的额
        button.tag = 100 + i;
        [self.tabBarView addSubview:button];
    }
    
    // 初始化选中图片的视图
    _selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
    _selectImageView.image = [UIImage imageNamed:@"home_bottom_tab_arrow"];
    [_tabBarView addSubview:_selectImageView];
}

- (void)btnAction:(UIButton *)sender{
    // 根据tag值判断当前的索引并赋值给当前的TabBarController
    self.selectedIndex = sender.tag - 100;
    
    // 添加滑动动画
    [UIView animateWithDuration:0.2 animations:^{
        // 中心点重合
        _selectImageView.center = sender.center;
    }];
}

- (void)showTabBar:(BOOL)show{
    CGRect frame = self.tabBarView.frame;
    if (show) {
        frame.origin.x = 0;
    }else{
        frame.origin.x = -screenWidth;
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        self.tabBarView.frame = frame;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end```

###首页 m

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #d12f1b}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa}p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #31595d}p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #78492a}p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #008400; min-height: 21.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s6 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s10 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s11 {font-variant-ligatures: no-common-ligatures; color: #31595d}

import "ProfieViewController.h"

import "ModelViewController.h"

import "RootViewController.h"

CGFloat const writeBtnWidth = 33;
CGFloat const writeBtnHeight = 32;

@interface ProfieViewController ()

@end

@implementation ProfieViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.title = @"首页";
    self.view.backgroundColor = [UIColor redColor];

    [self initNavButton];

    [self initPushButton];
    }

pragma mark - 自定义导航栏

  • (void)initNavButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, writeBtnWidth, writeBtnHeight);
    [button setBackgroundImage:[UIImage imageNamed:@"write"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(presentAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btnBBI = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = btnBBI;
    }

  • (void)presentAction{
    NSLog(@"首页左上角按钮");

    ModelViewController *m = [[ModelViewController alloc] init];
    [self presentViewController:m animated:YES completion:nil];
    }

  • (void)initPushButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 200, 40);

    [button setTitle:@"push" forState:UIControlStateNormal];
    // 此方法和setTitle两者只能二选一
    // [button setImage:<#(nullable UIImage *)#> forState:<#(UIControlState)#>]
    [button addTarget:self action:@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    }

  • (void)pushAction{
    NSLog(@"fdsalf");
    ModelViewController *model = [[ModelViewController alloc] init];
    // [self.navigationController pushViewController:model animated:YES];
    // iOS8新方法 和Push一样的效果
    [self.navigationController showViewController:model sender:nil];
    RootViewController *rootVC = (RootViewController *)self.tabBarController;
    [rootVC showTabBar:NO];
    }

pragma mark - 视图将要出现的时候出现标签栏

  • (void)viewWillAppear:(BOOL)animated{
    RootViewController *rootVC = (RootViewController *)self.tabBarController;
    [rootVC showTabBar:YES];
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    }
    */

@end```

效果;

tabBar.gif

相关文章

  • 三级控制器

    UITabBarController UINavigationController 一个导航控制器对应一个视图控制...

  • 三级控制器

    1.一个导航控制器对应于一个视图控制器, 2.标签控制器是管理固定的几个视图控制器,子控制器是并列的。每一个分栏控...

  • iOS 构造三级控制器

    前言:最近随着公司SDK的日趋稳定,终于能腾出时间来写一点技术上的问题。 何为三级视图控制器?即 标签控制器 ->...

  • 界面搭建 移除系统自带tabbar上的按钮

    修改导航栏上的背景和字体颜色移除系统tabbar上的按钮按钮的选中与取消 界面搭建一般都是三级控制器,标签控制器-...

  • iOS~如何优雅的创建一个三级联动界面

    如上图所示,这是一个相当复杂的三级联动界面,如果我们毫无章法的将代码堆积在父视图控制器中,无疑会使控制器变的臃肿,...

  • iOS Tabbar 隐藏状态诡异显示出来原因及解决方案

    一:问题出现的步骤: 我app的架构是一个常见的三级控制器: UITabbarController--->四个Na...

  • 影院项目总结

    三级控制器搭建都大同小异,这次的项目是使用storyboard搭建, 注意下在移除按钮的时候,那个方法应该写在vi...

  • iOS同时Dismiss两个控制器

    由A控制器present B控制器 再由B控制器present C控制器 在C控制器的时候 要求返回到A控制器,就...

  • 关于生命周期

    从A控制器 push B控制器 从B控制器 pop 回 A控制器

  • 浅谈控制器跳转(模态、push)

    情景:控制器A present: 控制器B present:控制器C:切换根控制器:控制器D 问题:如果 在C跳...

网友评论

    本文标题:三级控制器

    本文链接:https://www.haomeiwen.com/subject/jinqlttx.html