Swift ☞ 自定义UITabBarController
2016-06-29 22:30
651 查看
上代码咯。。。。
原文链接:http://blog.csdn.net/magiczyj/article/details/51786399
效果如下:
原文链接:http://blog.csdn.net/magiczyj/article/details/51786399
import UIKit class CustomTabBarBtn: UIButton { var mark:NSInteger = 0; } let SCREEN_W = UIScreen.mainScreen().bounds.size.width; let SCREEN_H = UIScreen.mainScreen().bounds.size.height; let TABBAR_H:CGFloat = 49.0; let TABBAR_BTN_W = SCREEN_W / 5.0; class MainViewController: UITabBarController { let backGroundImgV = UIImageView(); override func viewDidLoad() { super.viewDidLoad() foreDeclare(); creatTabBar(); } private func foreDeclare() { tabBar.hidden = true; backGroundImgV.userInteractionEnabled = true; } func creatTabBar() { backGroundImgV.image = UIImage(named: "backgroundimage.png"); backGroundImgV.frame = CGRect(x: 0, y: SCREEN_H - TABBAR_H, width: SCREEN_W, height: TABBAR_H); view.addSubview(backGroundImgV); for i in 0...4 { let tabBarButton = CustomTabBarBtn(); tabBarButton.frame = CGRect(x: TABBAR_BTN_W * CGFloat(i), y: 0, width: TABBAR_BTN_W, height: TABBAR_H); tabBarButton.setBackgroundImage(UIImage(named: "tabbar_\(i)"), forState: .Normal); tabBarButton.setBackgroundImage(UIImage(named: "tabbar_selected_\(i)"), forState: .Selected); tabBarButton.addTarget(self, action: "itemClick:", forControlEvents: .TouchUpInside); tabBarButton.mark = i; if i == 1 { tabBarButton.selected = true; } backGroundImgV.addSubview(tabBarButton); let vc = UIViewController(); vc.title = "页面\(i)"; let nav = UINavigationController(rootViewController: vc); addChildViewController(nav); } } func itemClick(btn: CustomTabBarBtn) { selectedIndex = btn.mark; for tabbarBtn in backGroundImgV.subviews { let item = tabbarBtn as! CustomTabBarBtn; if item.mark == btn.mark { item.selected = true; }else { item.selected = false; } } } 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. } */ }
效果如下:
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift自定义iOS中的TabBarController并为其添加动画
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- 使用 Swift 语言编写 Android 应用入门
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- Swift实现iOS应用中短信验证码倒计时功能的实例分享
- 使用Swift代码实现iOS手势解锁、指纹解锁实例详解
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift代码实现冒泡排序算法的简单实例
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符