VAAA VAAA - 1 year ago 61
Swift Question

How to create a custom view in the header of a form

I would like to know if its possible to create something like the image below.

enter image description here

I guess I have to create a custom view but any clue on how to do it?


Answer Source

If you have a UITableView on the screen, you can achieve this by creating a custom table header view:

  1. Create a subclass of UIView -> CustomTableHeaderView
  2. Add a button inside it:

    class CustomTableHeaderView: UIView {
        var rightButton: UIButton
        override init(frame: CGRect) {
            rightButton = UIButton()
            let buttonWidth: CGFloat = 100
            // this is hardcoded for cimplicity, it's better to setup auto layout constraints here
            rightButton.frame = CGRect(x: 0, y: frame.width - buttonWidth, width: buttonWidth, height: 50)
            rightButton.setTitle("My button", forState: .Normal)
            // other configuration
            super.init(frame: frame)
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")


    1. In your view controller (or any other place where you initialise a table view, set your custom view as a tableHeaderView:

    tableView.tableHeaderView = CustomTableHeaderView(frame: frame:CGRect(x: 0, y: 0, width: view.frame.width, height: 10))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download