重定义UITabBar

2016-09-18  本文已影响0人  潜水100号
Snip20160918_1.png

swift

主界面

class ViewController: UITabBarController {

  override func viewDidLoad() {
    super.viewDidLoad()
   
    var vcCtl1 = UIViewController()
    vcCtl1.view.backgroundColor = UIColor.redColor()
    vcCtl1.tabBarItem.title = "first"
   
    var vcCtl2 = UIViewController()
    vcCtl2.view.backgroundColor = UIColor.blueColor()
    vcCtl2.tabBarItem.title = "second"
   
    var vcCtl3 = UIViewController()
    vcCtl3.view.backgroundColor = UIColor.greenColor()
    vcCtl3.tabBarItem.title = "third"
   
    self.viewControllers = [vcCtl1,vcCtl2,vcCtl3]
   
    creatrUItabar()
  }
 
  func creatrUItabar(){
    var b = bar(frame: self.tabBar.bounds)
    //用新的将原来的tabBar覆盖掉
    self.setValue(b, forKey: "tabBar")
  }

重定义UITabBar

class bar: UITabBar {
 
  override init(frame: CGRect) {
    super.init(frame: frame)
    self.barTintColor = UIColor.blackColor()
    createUI()
   
  }
 
  func createUI(){
    for i in 1...2{
      //确定了大小上下
      var image = UIImageView(frame: CGRectMake(0, 5, 2, 40))
      image.image = UIImage(named: "1")
      //确定位置
      image.center = CGPointMake(((UIScreen.mainScreen().bounds.size.width)/3)*CGFloat(i), image.center.y)
      self.addSubview(image)
    }
   

  }
 
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}
上一篇下一篇

猜你喜欢

热点阅读