美文网首页
框架搭建_纯代码

框架搭建_纯代码

作者: PZcoder | 来源:发表于2017-03-13 11:58 被阅读1次

目录:
1、利用ViewController中间过渡
2、直接设置UITabBarController的数组

比较:相对来说第2中方式较方便,设置内容、标题等比较清晰明确,第1中方式中设置标题等内容时容易搞混,相对第2中方式没有较大的优势。

1、利用ViewController中间过渡

AppDelegate中代码设置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    
    return YES;
}

ViewController中代码设置

    //初始化要使用的三个VC
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //初始化三个nav
    UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:first];
    firstNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"111" image:nil tag:1];
    
    UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:second];
    secondNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"" image:nil tag:1];

    UINavigationController *thirdNav = [[UINavigationController alloc] initWithRootViewController:third];
    thirdNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"333" image:nil tag:1];

    //初始化UITabBarController
    self.tabBarCont = [[UITabBarController alloc] init];
    self.tabBarCont.viewControllers = @[firstNav,secondNav,thirdNav];
    
    self.tabBarCont.selectedIndex = 0;
    self.tabBarCont.delegate = self;
    self.tabBarCont.view.frame = self.view.frame;
    
    [self.view addSubview:self.tabBarCont.view];

各个VC中的设置

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"first";//此设置改变nav的标题和底部item的标题
    self.navigationController.title = @"111";//item标题
}

2、直接设置UITabBarController的数组

AppDelegate中设置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //初始化
    FirstVc *first = [[FirstVc alloc] init];
    SecondVc *second = [[SecondVc alloc] init];
    ThirdVc *third = [[ThirdVc alloc] init];
    
    //创建标签栏控制器
    tabBarControl = [UITabBarController new];
    
    //放入标签栏中
    tabBarControl.viewControllers = @[first,second,third];
    
    //创建导航栏控制器,并指定他的根视图控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:tabBarControl];
    
    //添加中间的按钮
    //缺点:1、按钮超过bottom的部分点击无响应    2、点击除按钮外的中间部分时也展示中间item的页面
    //3个item是可能范围太大,如果调整item数量,缺点2应该影响不大。
    UIButton *btn = [[UIButton alloc] init];
    btn.backgroundColor = [UIColor redColor];
    [btn setFrame:CGRectMake(130, -12, 60, 60)];
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 30;
    [btn addTarget:self action:@selector(clickCenterButton) forControlEvents:UIControlEventTouchUpInside];
    
    [tabBarControl.tabBar addSubview:btn];
    
    //指定应用的跟视图控制器
    self.window.rootViewController = nav;
    
    return YES;
}

各个VC中的设置

- (instancetype)init
{
    self = [super init];
    
    if (self)
    {
        self.title = @"111";//此处的title是item的标题
        
        //设置图标的默认图片和选中后图片
        self.tabBarItem.image = [[UIImage imageNamed:@"tabbar_mainframe@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_mainframeHL@2x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //未读消息数量(右上角标识)
        [self.tabBarItem setBadgeValue:@"18"];
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.navigationItem.title = @"first";//共同使用一个navigation,要在此方法中设置名称
    
    //设置右边导航按钮
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(tapAdd)];

    self.tabBarController.navigationItem.rightBarButtonItem = rightItem;
}

- (void)tapAdd
{
    //此处跳转页面操作
    //或者弹出页面
}

相关文章

  • 框架搭建_纯代码

    目录:1、利用ViewController中间过渡2、直接设置UITabBarController的数组 比较:相...

  • 02-项目搭建

    项目搭建 课程目标 熟悉 Swift 语法 搭建系统主体框架结构 对比与 OC 开发的异同 纯代码搭建框架 创建文...

  • 纯代码搭建IOS主流框架

    1、WYYNavigationController #import"WYYNavigationController...

  • 收藏整理(持续更新)

    项目搭建 优雅快速的搭建一个IOS项目,让你的项目结构不再混乱!(纯代码) 手把手教你如何搭建iOS项目基本框架 ...

  • Gin+ini+redis+log+mysql+cron+gov

    最近学习Go,在看各种文档,博客,别人写的代码。最后搭建了一个纯API应用框架。 选用Gin框架做web服务 分层...

  • 分分钟搭建App主流框架_纯代码搭建(OC)

    搭建主流框架界面 导读 我们玩iPhone应用的时候,有没发现大部分的应用都是上图差不多的结构,下面的TabBar...

  • TabBar中间按钮设置

    类似iOS简书中间发布文章用的圆形按钮1、在《框架搭建_纯代码》的基础上稍作修改2、自定义几个页面,用button...

  • 工作使用git

    项目经理 项目经理搭建项目框架 搭建项目框架之后,项目经理把项目框架代码放到服务器上或者github上面 员工 在...

  • 2021-07-26

    fc代码框架搭建 熟悉验证的流程 1、代码框架 问题:forward函数里面的参数计算和kernel重排,还有些不...

  • 手写代码搭建框架

    TabBarController--4 * NavigationController (TabBarItem/Vi...

网友评论

      本文标题:框架搭建_纯代码

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