iOS Swift 自定义tabbar
2015-01-04 14:57
267 查看
在iOS中
我们首先创建出4个视图控制器,跟一个主视图控制器
MainViewController
.h
//
// MainViewController.h
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MainViewController : UITabBarController
//自定义tabbar
@property(nonatomic, strong)UIImageView *tabbarImg;
@end
一般来说自定义的tabbar都是图片视图来实现的
//
// MainViewController.m
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import "MainViewController.h"
#import "ViewqController.h"
#import "ViewwController.h"
#import "VieweController.h"
#import "ViewrController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//自定义tabbar
[self _initTabbar];
//加载视图
[self _initView];
}
//自定义tabbar
- (void)_initTabbar
{
//隐藏系统的tabbar按钮
self.tabBar.hidden = YES;
//创建自定义tabbar
_tabbarImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-49, 320, 49)];
//UIImageView一定要打开用户交互,不然会阻止按钮事件的传递
_tabbarImg.userInteractionEnabled = YES;
_tabbarImg.backgroundColor = [UIColor grayColor];
//tabbar上的按钮
for (int i = 0; i<4; i++) {
UIButton *BT = [UIButton buttonWithType:UIButtonTypeCustom];
[BT setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
BT.frame = CGRectMake(self.view.bounds.size.width/4*i, 0, self.view.bounds.size.width/4, 49);
BT.tag = i;
[BT addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
[_tabbarImg addSubview:BT];
}
[self.view addSubview:_tabbarImg];//加入视图控制器
}
//加载视图
- (void)_initView
{
ViewqController *vc1 = [[ViewqController alloc] init];
ViewwController *vc2 = [[ViewwController alloc] init];
VieweController *vc3 = [[VieweController alloc] init];
ViewrController *vc4 = [[ViewrController alloc] init];
NSArray *subs = @[vc1, vc2, vc3, vc4];
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
//如果将导航栏给到程序入口那么push进去将不显示导航栏,这样的话push进去会有导航栏,可以根据需求来决定是否隐藏显示
for (int i = 0; i<4; i++) {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:subs[i]];
[arr addObject:nav];
}
//将所有子视图加入视图控制器
[self setViewControllers:arr];
}
//更换tabbar
- (void)buttonclick:(UIButton *)button
{
//这里来更换视图控制器
self.selectedIndex = button.tag;
}
- (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
Main继承于TabBar视图将所有子视图控制器加入其中根据按钮的tag值来切换视图
//我们如何在其他的视图中来操作这个自定义的tabbar呢?
如下
.h
//
// HiddenTabBar.h
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HiddenTabBar : NSObject
+ (void)hidden;//类方法。隐藏掉tabbar
+ (void)show;//类方法。显示tabbar
@end
.m
//
// HiddenTabBar.m
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import "HiddenTabBar.h"
#import "AppDelegate.h"
#import "MainViewController.h"
@implementation HiddenTabBar
+ (void)hidden
{
AppDelegate *app = [[UIApplication sharedApplication]delegate];
MainViewController *main = (MainViewController *)app.window.rootViewController;
main.tabbarImg.hidden = YES;
}
+ (void)show
{
AppDelegate *app = [[UIApplication sharedApplication]delegate];
MainViewController *main = (MainViewController *)app.window.rootViewController;
main.tabbarImg.hidden = NO;
}
@end
然后直接在需要用的地方调用就好了
//点击隐藏tabbar
- (void)click
{
[HiddenTabBar hidden];
}
//点击显示tabbar
- (void)click2
{
[HiddenTabBar show];
}
//自定义的导航栏下次在写吧
在swift中自定义tabbar
同道理自己写出window控制器,删掉系统自带的故事版
AppDelegate
//
// AppDelegate.swift
// 自定义tabbar导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
//加入根视图控制器
var main = MainViewController()
self.window?.rootViewController = main
self.window!.makeKeyAndVisible()
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
将Main设置为根视图,道理跟在iOS中一样,Main继承于tabBar视图
Main
//
// MainViewController.swift
// 自定义tabbar导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
import UIKit
class MainViewController: UITabBarController {
//自定义tabbar
var tabbarImg:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
ae77
//调用自定义tabbarr
self ._initTabbar()
// Do any additional setup after loading the view.
}
//自定义tabbar
func _initTabbar(){
self.tabBar.hidden = true
tabbarImg = UIImageView(frame: CGRectMake(0, self.view.bounds.size.height-49, 320, 49))
//UIImageView必须要打开用户交互
tabbarImg.userInteractionEnabled = true
tabbarImg.backgroundColor = UIColor.grayColor()
//自定义tabbar上边的按钮
var bt1 = UIButton(frame: CGRectMake(0, 0, 80, 49))
bt1 .setTitle("1", forState: .Normal)
bt1.tag = 0
bt1 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt1)
var bt2 = UIButton(frame: CGRectMake(80, 0, 80, 49))
bt2 .setTitle("2", forState: .Normal)
bt2.tag = 1
bt2 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt2)
var bt3 = UIButton(frame: CGRectMake(160, 0, 80, 49))
bt3 .setTitle("3", forState: .Normal)
bt3.tag = 2
bt3 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt3)
var bt4 = UIButton(frame: CGRectMake(240, 0, 80, 49))
bt4 .setTitle("4", forState: .Normal)
bt4.tag = 3
bt4 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt4)
self.view .addSubview(tabbarImg)
//初始化所有视图控制器
var vc1 = ViewqController()
var vc2 = ViewwController()
var vc3 = VieweController()
var vc4 = ViewrController()
//将其加入数组
var arr:Array = [vc1, vc2, vc3, vc4]
var subs = NSMutableArray()
for var i = 0; i<4; ++i{
//加入导航
var nav = UINavigationController(rootViewController: arr[i])
subs .addObject(nav)
}
self .setViewControllers(subs, animated: true)
}
//swift函数
func click(bt:UIButton)
{
println("\(bt.tag)")
//根据tag值来变化视图控制器
self.selectedIndex = bt.tag
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
道理也是跟iOS一样,使用button的tag值来切换视图控制器
我们首先创建出4个视图控制器,跟一个主视图控制器
MainViewController
.h
//
// MainViewController.h
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MainViewController : UITabBarController
//自定义tabbar
@property(nonatomic, strong)UIImageView *tabbarImg;
@end
一般来说自定义的tabbar都是图片视图来实现的
//
// MainViewController.m
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import "MainViewController.h"
#import "ViewqController.h"
#import "ViewwController.h"
#import "VieweController.h"
#import "ViewrController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//自定义tabbar
[self _initTabbar];
//加载视图
[self _initView];
}
//自定义tabbar
- (void)_initTabbar
{
//隐藏系统的tabbar按钮
self.tabBar.hidden = YES;
//创建自定义tabbar
_tabbarImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-49, 320, 49)];
//UIImageView一定要打开用户交互,不然会阻止按钮事件的传递
_tabbarImg.userInteractionEnabled = YES;
_tabbarImg.backgroundColor = [UIColor grayColor];
//tabbar上的按钮
for (int i = 0; i<4; i++) {
UIButton *BT = [UIButton buttonWithType:UIButtonTypeCustom];
[BT setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
BT.frame = CGRectMake(self.view.bounds.size.width/4*i, 0, self.view.bounds.size.width/4, 49);
BT.tag = i;
[BT addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside];
[_tabbarImg addSubview:BT];
}
[self.view addSubview:_tabbarImg];//加入视图控制器
}
//加载视图
- (void)_initView
{
ViewqController *vc1 = [[ViewqController alloc] init];
ViewwController *vc2 = [[ViewwController alloc] init];
VieweController *vc3 = [[VieweController alloc] init];
ViewrController *vc4 = [[ViewrController alloc] init];
NSArray *subs = @[vc1, vc2, vc3, vc4];
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:4];
//如果将导航栏给到程序入口那么push进去将不显示导航栏,这样的话push进去会有导航栏,可以根据需求来决定是否隐藏显示
for (int i = 0; i<4; i++) {
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:subs[i]];
[arr addObject:nav];
}
//将所有子视图加入视图控制器
[self setViewControllers:arr];
}
//更换tabbar
- (void)buttonclick:(UIButton *)button
{
//这里来更换视图控制器
self.selectedIndex = button.tag;
}
- (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
Main继承于TabBar视图将所有子视图控制器加入其中根据按钮的tag值来切换视图
//我们如何在其他的视图中来操作这个自定义的tabbar呢?
如下
.h
//
// HiddenTabBar.h
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HiddenTabBar : NSObject
+ (void)hidden;//类方法。隐藏掉tabbar
+ (void)show;//类方法。显示tabbar
@end
.m
//
// HiddenTabBar.m
// 自定义tabbar跟导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
#import "HiddenTabBar.h"
#import "AppDelegate.h"
#import "MainViewController.h"
@implementation HiddenTabBar
+ (void)hidden
{
AppDelegate *app = [[UIApplication sharedApplication]delegate];
MainViewController *main = (MainViewController *)app.window.rootViewController;
main.tabbarImg.hidden = YES;
}
+ (void)show
{
AppDelegate *app = [[UIApplication sharedApplication]delegate];
MainViewController *main = (MainViewController *)app.window.rootViewController;
main.tabbarImg.hidden = NO;
}
@end
然后直接在需要用的地方调用就好了
//点击隐藏tabbar
- (void)click
{
[HiddenTabBar hidden];
}
//点击显示tabbar
- (void)click2
{
[HiddenTabBar show];
}
//自定义的导航栏下次在写吧
在swift中自定义tabbar
同道理自己写出window控制器,删掉系统自带的故事版
AppDelegate
//
// AppDelegate.swift
// 自定义tabbar导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.whiteColor()
//加入根视图控制器
var main = MainViewController()
self.window?.rootViewController = main
self.window!.makeKeyAndVisible()
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
将Main设置为根视图,道理跟在iOS中一样,Main继承于tabBar视图
Main
//
// MainViewController.swift
// 自定义tabbar导航栏
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//
import UIKit
class MainViewController: UITabBarController {
//自定义tabbar
var tabbarImg:UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
ae77
//调用自定义tabbarr
self ._initTabbar()
// Do any additional setup after loading the view.
}
//自定义tabbar
func _initTabbar(){
self.tabBar.hidden = true
tabbarImg = UIImageView(frame: CGRectMake(0, self.view.bounds.size.height-49, 320, 49))
//UIImageView必须要打开用户交互
tabbarImg.userInteractionEnabled = true
tabbarImg.backgroundColor = UIColor.grayColor()
//自定义tabbar上边的按钮
var bt1 = UIButton(frame: CGRectMake(0, 0, 80, 49))
bt1 .setTitle("1", forState: .Normal)
bt1.tag = 0
bt1 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt1)
var bt2 = UIButton(frame: CGRectMake(80, 0, 80, 49))
bt2 .setTitle("2", forState: .Normal)
bt2.tag = 1
bt2 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt2)
var bt3 = UIButton(frame: CGRectMake(160, 0, 80, 49))
bt3 .setTitle("3", forState: .Normal)
bt3.tag = 2
bt3 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt3)
var bt4 = UIButton(frame: CGRectMake(240, 0, 80, 49))
bt4 .setTitle("4", forState: .Normal)
bt4.tag = 3
bt4 .addTarget(self, action: "click:", forControlEvents: UIControlEvents.TouchUpInside)
tabbarImg .addSubview(bt4)
self.view .addSubview(tabbarImg)
//初始化所有视图控制器
var vc1 = ViewqController()
var vc2 = ViewwController()
var vc3 = VieweController()
var vc4 = ViewrController()
//将其加入数组
var arr:Array = [vc1, vc2, vc3, vc4]
var subs = NSMutableArray()
for var i = 0; i<4; ++i{
//加入导航
var nav = UINavigationController(rootViewController: arr[i])
subs .addObject(nav)
}
self .setViewControllers(subs, animated: true)
}
//swift函数
func click(bt:UIButton)
{
println("\(bt.tag)")
//根据tag值来变化视图控制器
self.selectedIndex = bt.tag
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
道理也是跟iOS一样,使用button的tag值来切换视图控制器
相关文章推荐
- ios-swift 自定义TabBarViewController
- iOS&Swift自定义的tabbar出现重叠
- IOS之自定义tabbar
- ios 仿淘宝自定义TabBar
- IOS 自定义TabBar选中时为蓝色解决方法
- 猫猫学iOS(四十三)之网易彩票底部自定义TabBar实现切换
- IOS(swift)自定义UI控件的实例(跟随手指运动的小球)
- ios开发笔记之 自定义tabBar
- 从0开始学习Swift开发IOS应用(3)——TabBar
- IOS-自定义TabBar
- iOS 自定义tabbar 关于push问题 小技巧
- 【swift,oc】ios开发中巧用自动布局设置自定义cell的高度
- IOS开发笔记_4自定义TabBar
- iOS 原生UITabBarController 的tabbar 图标状态自定义
- ios学习笔记----实现一个带滑动手势的tabBarViewController,并可自定义tabBar
- [iOS微博项目 - 1.6] - 自定义TabBar
- iOS开发笔记_4自定义TabBar
- iOS自定义tabbar,可以隐藏
- swift 自定义TabBarItem
- iOS通过按钮图片创建自定义TabBar