Tabbar中间添加个按钮

tabbar 绑定上 UITabBarController

    override func viewDidLoad() {
        super.viewDidLoad()

        self.addCenterButton(btnimage: UIImage(named: "tx")!, selectedbtnimg: UIImage(named: "txxx")!, selector: "addOrderView", view: self.view)
    }
    func addCenterButton(btnimage buttonImage:UIImage,selectedbtnimg selectedimg:UIImage,selector:String,view:UIView)
    {
        //创建一个自定义按钮
        let button:UIButton = UIButton(type: UIButtonType.custom)
        //btn.autoresizingMask
        //button大小为适应图片
        button.frame = CGRect(x: 0,y: 0,width: buttonImage.size.width,height:  buttonImage.size.height);
        button.setImage(buttonImage, for: UIControlState.normal)
        button.setImage(selectedimg, for: UIControlState.selected)
        //去掉阴影
        button.adjustsImageWhenDisabled = true;
        //按钮的代理方法
        button.addTarget( self, action: Selector(selector), for:UIControlEvents.touchUpInside )
        //高度差
        let heightDifference:CGFloat = buttonImage.size.height - self.tabBar.frame.size.height
        if (heightDifference < 0){
            button.center = self.tabBar.center;
        }
        else
        {
            var center:CGPoint = self.tabBar.center;
            center.y = center.y - heightDifference/2.0;
            button.center = center;
        }
        view.addSubview(button);
    }
    
    
    //按钮方法
    func addOrderView()
    {
        print("执行")
    }

你可能感兴趣的:(Tabbar中间添加个按钮)