自学iOS开发系列----UI(视图编程入门:UITabBarController)

本章教学效果:
HTTabBarController

核心代码
封装工具类Tools:
HTTools.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface HTTools : NSObject

//工厂模式:想要创建一个Button
+ (UIButton *)createButton:(CGRect)frame bgColor:(UIColor *)bgColor title:(NSString *)title titleColor:(UIColor *)titleColor tag:(NSInteger)tag action:(SEL)action vc:(id)vc;

//创建标签
+ (UILabel *)createLabel:(CGRect)frame text:(NSString *)text textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)textColor bgColor:(UIColor *)bgColor tag:(NSInteger)tag;

@end

HTTools.m

#import "HTTools.h"

@implementation HTTools

+ (UIButton *)createButton:(CGRect)frame bgColor:(UIColor *)bgColor title:(NSString *)title titleColor:(UIColor *)titleColor tag:(NSInteger)tag action:(SEL)action vc:(id)vc {
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    button.backgroundColor = bgColor;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.tag = tag;
    [button addTarget:vc action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UILabel *)createLabel:(CGRect)frame text:(NSString *)text textAlignment:(NSTextAlignment)textAlignment textColor:(UIColor *)textColor bgColor:(UIColor *)bgColor tag:(NSInteger)tag {
    UILabel * label = [[UILabel alloc] init];
    label.frame = frame;
    label.text = text;
    label.textAlignment = textAlignment;
    label.textColor = textColor;
    label.backgroundColor = bgColor;
    label.tag = tag;
    return label;
}

@end

AppDelegate.m

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    /**
     *  标签栏控制器UITabBarController
     *  标签栏控制器的工作原理和导航控制器一样,都是用来管理子视图控制器之间的层次关系
     *
     *   不同点:导航控制器使用的是压栈和出栈,但是标签栏控制器上的所有子视图控制器层次上是平级的,就是说所有的子视图控制器平铺在标签栏控制器之上
     */
    RootViewController * root = [[RootViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:root];
    //为root上的导航条添加标题,导航条上的内容必须通过导航控制器上的子视图控制器对象调用navigationItem的属性设置
    root.navigationItem.title = @"首页";
    //同样的为标签栏控制器上的视图添加标题 使用标签栏控制器上的子视图控制器对象调用tabBarItem.title
    nav.tabBarItem.title = @"界面1";

    SecondViewController * second = [[SecondViewController alloc] init];
    second.tabBarItem.title = @"界面2";

    ThirdViewController * third = [[ThirdViewController alloc] init];
    third.tabBarItem.title = @"界面3";

    FourViewController * four = [[FourViewController alloc] init];
    four.tabBarItem.title = @"界面4";

    //创建标签栏控制器对象
    UITabBarController * tabBar = [[UITabBarController alloc] init];
    //向tabBarController上添加子视图控制器
    tabBar.viewControllers = @[nav,second,third,four];
    self.window.rootViewController = tabBar;
    [self.window makeKeyAndVisible];

demo地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值