您的位置:首页 > 产品设计 > UI/UE

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

2017-03-09 10:46 471 查看
本章教学效果:



核心代码

封装工具类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地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐