 
 class ViewController: NSViewController {
 
+    let backgroundColor = NSColor(red: 247/255.0, green: 247/255.0, blue: 247/255.0, alpha: 1)
+    let touchBarWidth:CGFloat = 1005.0
+    
     let dinoView: DinoView = DinoView()
     let simpleTextField: NSTextField = NSTextField(labelWithString: "JAY IS SMART")
-    let overlayButton = NSButton(title: " ", target: self, action: #selector(jump))
+    let mainTapReceiverButton = NSButton(title: " ", target: self, action: #selector(tap))
     
     override func viewDidLoad() {
         super.viewDidLoad()
 
         // Do any additional setup after loading the view.
-        self.dinoView.wantsLayer = true
-        self.dinoView.layer?.backgroundColor = NSColor(red: 0.3, green: 0.3, blue: 0.85, alpha: 1).cgColor
-        self.dinoView.addSubview(simpleTextField)
-        self.dinoView.addSubview(overlayButton)
-        
-        self.overlayButton.frame = NSRect(x: 0, y: 0, width: 1005, height: 30)
-        self.overlayButton.isTransparent = true
-        
+        setupDinoView()
+        setupMainTapReceiverButton()
     }
     
-    func jump() {
+    func tap() {
         print("TAPPED")
+    }
+    
+    func setupDinoView() {
+        let c1 = NSLayoutConstraint(item: dinoView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: touchBarWidth)
+        dinoView.addConstraint(c1)
+        
+        dinoView.wantsLayer = true
+        dinoView.layer?.backgroundColor = backgroundColor.cgColor
+        
+        dinoView.addSubview(simpleTextField)
+        dinoView.addSubview(mainTapReceiverButton)
+    }
+    
+    func setupDinoViewOnAppear() {
+        
+        if let touchBarView = dinoView.superview {
+            
+            let c1 = NSLayoutConstraint(item: dinoView, attribute: .leading, relatedBy: .equal, toItem: touchBarView, attribute: .leading, multiplier: 1.0, constant: 0)
+            let c2 = NSLayoutConstraint(item: dinoView, attribute: .trailing, relatedBy: .equal, toItem: touchBarView, attribute: .trailing, multiplier: 1.0, constant: 0)
+            let c3 = NSLayoutConstraint(item: dinoView, attribute: .top, relatedBy: .equal, toItem: touchBarView, attribute: .top, multiplier: 1.0, constant: 0)
+            let c4 = NSLayoutConstraint(item: dinoView, attribute: .bottom, relatedBy: .equal, toItem: touchBarView, attribute: .bottom, multiplier: 1.0, constant: 0)
+            
+            touchBarView.addConstraint(c1)
+            touchBarView.addConstraint(c2)
+            touchBarView.addConstraint(c3)
+            touchBarView.addConstraint(c4)
+        } else {
+            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
+                self.setupDinoView()
+            })
+        }
+    }
+    
+    func setupMainTapReceiverButton() {
+        mainTapReceiverButton.isTransparent = true
+        mainTapReceiverButton.translatesAutoresizingMaskIntoConstraints = false
+        
+        let c1 = NSLayoutConstraint(item: mainTapReceiverButton, attribute: .leading, relatedBy: .equal, toItem: dinoView, attribute: .leading, multiplier: 1.0, constant: 0)
+        let c2 = NSLayoutConstraint(item: mainTapReceiverButton, attribute: .trailing, relatedBy: .equal, toItem: dinoView, attribute: .trailing, multiplier: 1.0, constant: 0)
+        let c3 = NSLayoutConstraint(item: mainTapReceiverButton, attribute: .top, relatedBy: .equal, toItem: dinoView, attribute: .top, multiplier: 1.0, constant: 0)
+        let c4 = NSLayoutConstraint(item: mainTapReceiverButton, attribute: .bottom, relatedBy: .equal, toItem: dinoView, attribute: .bottom, multiplier: 1.0, constant: 0)
+        
+        dinoView.addConstraint(c1)
+        dinoView.addConstraint(c2)
+        dinoView.addConstraint(c3)
+        dinoView.addConstraint(c4)
     }
 
     override var representedObject: Any? {

        case NSTouchBarItemIdentifier.dinoItem:
            let customViewItem = NSCustomTouchBarItem(identifier: identifier)
            customViewItem.view = dinoView
            return customViewItem
        default:
            return nil


}

@available(OSX 10.12.1, *)
extension ViewController: NSTouchBarDelegate {
    override func makeTouchBar() -> NSTouchBar? {
        let touchBar = NSTouchBar()
        touchBar.delegate = self
        touchBar.customizationIdentifier = .dinoBar
        touchBar.defaultItemIdentifiers = [.dinoItem]
        touchBar.customizationAllowedItemIdentifiers = [.dinoItem]
        return touchBar
    }
