how to create bar button or UIButton in tableview header in swift 2.2? [closed]
Asked Answered
P

2

7

I want to create a UIBarButtonItem or UIButton in UITableview header so when I click on the UITableview header button it should go to the other UIViewcontroller. How can I achieve this programmatically?

Prototype answered 8/8, 2016 at 8:46 Comment(4)
can you show the screen shot which type u needRattat
please add code or pictures of what you have done so farCoorg
why you choose tableheader , directly add in navigation bar right buttonRattat
i already taken two button on the navigation bar.Prototype
R
8

do like

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
{

initially get the frame of your Tableview

let frame: CGRect = tableView.frame

set the frame for UIbutton where is comes in View

let DoneBut: UIButton = UIButton(frame: CGRectMake(100, 0, 200, 50)) //frame.size.width - 60
DoneBut.setTitle("Done", forState: .Normal)
DoneBut.backgroundColor = UIColor.redColor()

if you are using swift 2.2 call the selector as follows

 DoneBut.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)

if you are using swift 2.1 call the selector as follows

DoneBut.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: .TouchUpInside)button.addTarget(self, action: Selector("buttonTapped:"), forControlEvents: .TouchUpInside)

create the UIView reason viewforHeader returns UIView and add button to that subView

 let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
headerView.addSubview(DoneBut)
return headerView
}

call method like

func buttonTapped(sender: UIButton) {
//Button Tapped and open your another ViewController

}

Updated answer

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{

    let frame: CGRect = tableView.frame


    let DoneBut: UIButton = UIButton(frame: CGRectMake(frame.size.width - 200, 0, 150, 50)) //
    DoneBut.setTitle("Done", forState: .Normal)
    DoneBut.backgroundColor = UIColor.redColor()

    DoneBut.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), forControlEvents: .TouchUpInside)

    DoneBut.backgroundColor = UIColor.blueColor()

    let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.width, frame.size.height))
      headerView.backgroundColor = UIColor.redColor()
    headerView.addSubview(DoneBut)
    return headerView
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50.0
}

func buttonTapped(sender: UIButton) {
    //Button Tapped and open your another ViewController

}

output

enter image description here

Rattat answered 8/8, 2016 at 8:53 Comment(8)
table view is coming white button is not visiblePrototype
try this let DoneBut: UIButton = UIButton(frame: CGRectMake(100, 0, 200, 50)) Rattat
i given the header background color also but still nothing is comingPrototype
can you send the project I wil checkRattat
@Prototype - what is this , else show the full codeRattat
sorry, where can i sent you my code??Prototype
Let us continue this discussion in chat.Prototype
That is what I want! Is it better to put the header view and button initialization code outside of viewForHeaderInSection? e.g. use lazy var closure to create this header view.Matsumoto
P
2

Hi guys this is working for.May be this helpful for you.

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
   return 50
}


func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
   let frame: CGRect = tableView.frame
   let DoneBut: UIButton = UIButton(frame: CGRectMake(200, 0, 200, 50))
   DoneBut.setTitle("+", forState: .Normal)
   DoneBut.backgroundColor = UIColor.blueColor()
   DoneBut.addTarget(self, action: #selector(DetailViewController.pressed(_:)), forControlEvents: .TouchUpInside)
   let headerView: UIView = UIView(frame: CGRectMake(0, 0, frame.size.height, frame.size.width))
   headerView.addSubview(DoneBut)
   return headerView
}


func pressed(sender: UIButton!) {
  print("Hello");    
  let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)    
  let qutationViewController = storyBoard.instantiateViewControllerWithIdentifier("newWorkViewController") as! NewWorkViewController
  self.navigationController?.pushViewController(qutationViewController, animated: true)            
}
Prototype answered 8/8, 2016 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.