         let frame = self.frame ?? CGRectMake(0, 0, 38, 38)
         var buttonCustomView: UIView
         if let text = self.text {
-            let label = UILabel(frame: frame)
+            let label = UILabel()
             label.backgroundColor = UIColor.clearColor()
-            label.textColor = self.tintColor
+            label.textColor = self.textColor ?? self.tintColor
             label.font = self.font ?? UIFont.systemFontOfSize(UIFont.buttonFontSize())
             label.text = text
             label.textAlignment = .Center
             label.minimumScaleFactor = 0.2
             label.adjustsFontSizeToFitWidth = true
             buttonCustomView = label
+            self.label = label
         } else {
-            let imageView = UIImageView(frame: frame)
+            let imageView = UIImageView()
             imageView.backgroundColor = UIColor.clearColor()
             imageView.image = icon
             imageView.contentMode = self.contentMode ?? .ScaleAspectFit
             imageView.tintColor = tintColor
             buttonCustomView = imageView
-        }
-        
-        let button = UIButton(type: UIButtonType.Custom)
+            self.imageView = imageView
+        }
+        
+        let button = NotifyingButton(type: UIButtonType.Custom)
+        button.onDidMoveToSuperView = { [weak self] (notifyingButton, superview) in
+            //            guard let superview = superview else {
+            //                return
+            //            }
+            
+            guard let welf = self else {
+                return
+            }
+            
+            //            let isRightButton = notifyingButton.frame.origin.x > superview.frame.width / 2
+            //            welf.label?.textAlignment = isRightButton ? .Right : .Left
+            
+            welf.onDidMoveToSuperViewCallback?(animatedBarButtonItem: welf)
+        }
+        
         button.frame = frame
+        buttonCustomView.frame = button.bounds
         button.layer.cornerRadius = 5
         button.layer.borderWidth = 0
+        button.autoresizingMask = UIViewAutoresizing.FlexibleHeight.union(.FlexibleWidth)
         button.addSubview(buttonCustomView)
         button.addTarget(self, action: #selector(tapAction(_:)), forControlEvents: .TouchUpInside)
-        button.translatesAutoresizingMaskIntoConstraints = false
-        NSLayoutConstraint.fullscreenLayoutForView(viewToLayout: buttonCustomView, inView: button)
         button.setNeedsUpdateConstraints()
         button.setNeedsLayout()
         button.setNeedsDisplay()

//  Wishlist
//
//  Created by Christian Deckert on 15.11.16.
//  Copyright © 2016 Christian Deckert
//

import Foundation
import UIKit

class CDAnimatedBarButtonItem: UIBarButtonItem {
    
    public var onTapCallback: ((animatedBarButtonItem: CDAnimatedBarButtonItem?) -> Void)?
    public var rippleColor: UIColor = UIColor.blackColor()
    public var font: UIFont?
    public var contentMode: UIViewContentMode?
    
    @IBInspectable public var frame: CGRect? {
        didSet {
            updateUI()
        }
    }
    
    @IBInspectable public var icon: UIImage? {
        didSet {
            updateUI()
        }
    }
    
    @IBInspectable public var text: String? {
        didSet {
            updateUI()
        }
    }
    
    public override var title: String? {
        get {
            return self.text
        }
        
        set {
            self.text = newValue
            updateUI()
        }
    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    public override init() {

            return
        }
        
        let halfDuration = Double(self.options.animationDuration/2)
        UIView.animateWithDuration(halfDuration, animations: {
            subview.transform = CGAffineTransformMakeScale(self.options.animationScaleFactor, self.options.animationScaleFactor)
            button.backgroundColor = self.options.rippleColor
        }) { complete in
            
            self.onTapCallback?(animatedBarButtonItem: self)
            
            UIView.animateWithDuration(halfDuration, animations: {
                button.backgroundColor = UIColor.clearColor()
                subview.transform = CGAffineTransformIdentity
            }) { complete in
