title: Swift - How creating custom viewForHeaderInSection, Using a XIB file?
body: <p>I can create simple custom viewForHeaderInSection in programmatically like below. But I want to do much more complex things maybe connection with a different class and reach their properties like a tableView cell. Simply, I want to see what I do. </p>
  
  <pre><code>func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
  
      if(section == 0) {
  
          let view = UIView() // The width will be the same as the cell, and the height should be set in tableView:heightForRowAtIndexPath:
          let label = UILabel()
          let button   = UIButton(type: UIButtonType.System)
  
          label.text="My Details"
          button.setTitle("Test Title", forState: .Normal)
          // button.addTarget(self, action: Selector("visibleRow:"), forControlEvents:.TouchUpInside)
  
          view.addSubview(label)
          view.addSubview(button)
  
          label.translatesAutoresizingMaskIntoConstraints = false
          button.translatesAutoresizingMaskIntoConstraints = false
  
          let views = ["label": label, "button": button, "view": view]
  
          let horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|", options: .AlignAllCenterY, metrics: nil, views: views)
          view.addConstraints(horizontallayoutContraints)
  
          let verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
          view.addConstraint(verticalLayoutContraint)
  
          return view
      }
  
      return nil
  }
  
  
  func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
      return 50
  }
  </code></pre>
  
  <p>Is there anyone to explain how can I create a custom tableView header view using xib? I have encountered with old Obj-C topics but I'm new with Swift language. If someone explain as detailed, It would be great.</p>
  
  <blockquote>
    <p><strong>1.issue:</strong> Button @IBAction doesn't connect with my ViewController. <strong>(Fixed)</strong></p>
  </blockquote>
  
  <p>Solved with File's Owner, ViewController base class (clicked left outline menu.)</p>
  
  <blockquote>
    <p><strong>2.issue:</strong> Header height problem <strong>(Fixed)</strong></p>
  </blockquote>
  
  <p>Solved adding <strong>headerView.clipsToBounds = true</strong> in <strong>viewForHeaderInSection</strong>: method.</p>
  
  <p><strong>For constraint warnings</strong> <a href="https://stackoverflow.com/questions/11664115/unable-to-simultaneously-satisfy-constraints-will-attempt-to-recover-by-breakin/30096600#30096600">this answer solved my problems</a>: </p>
  
  <p>When I added ImageView even same height constraint with this method in viewController, it flow over tableView rows <a href="https://i.stack.imgur.com/yoYQ6.png" rel="noreferrer">look like picture</a>.</p>
  
  <pre><code> func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
      return 120
  }
  </code></pre>
  
  <p>If I use, automaticallyAdjustsScrollViewInsets in viewDidLoad, In this case image flows under navigationBar. -fixed-</p>
  
  <pre><code>self.automaticallyAdjustsScrollViewInsets = false
  </code></pre>
  
  <blockquote>
    <p><strong>3.issue:</strong> If button under View <strong>(Fixed)</strong></p>
  </blockquote>
  
  <pre><code>@IBAction func didTapButton(sender: AnyObject) {
      print("tapped")
  
      if let upView = sender.superview {
          if let headerView = upView?.superview as? CustomHeader {
              print("in section \(headerView.sectionNumber)")
          }
  
      }
  }
  </code></pre>