您的位置:首页 > 移动开发 > Swift

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值来切换视图控制器
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息