Krutarth Patel Krutarth Patel - 1 month ago 3x
iOS Question

UiButton Position not change programmatically ios

i am doing autosizing tableview like tableview height will change according to its content.for that i am using this code

override func viewWillLayoutSubviews() {
var frame = self.tblReceipt.frame
frame.size.height = self.tblReceipt.contentSize.height
self.tblReceipt.frame = frame

and i am able to do this functionality.

than i am adding button bottom to tableview.
to set top position from tableview i am using this code

let pinTop = NSLayoutConstraint(item: self.btnPrint, attribute: .Top, relatedBy: .Equal,
toItem: self.tblReceipt, attribute: .Bottom, multiplier: 1.0, constant: 10)

self.btnPrint.translatesAutoresizingMaskIntoConstraints = false


i am doing this because whatever size of tableview is there.button should be always top from tableview.

but this is not are screenshots of i am getting error

enter image description here

enter image description here

UITableView Delegate Method

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrReceipt.count

Data Source method

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:PrintCell = (tblReceipt.dequeueReusableCellWithIdentifier("PrintCell"))! as! PrintCell
cell.lblProductName.text = arrReceipt[indexPath.row].objectForKey("product_name") as? String
cell.lblProductPrice.text = String(format:"%.2f", arrReceipt[indexPath.row]["product_price"]!!.doubleValue)

return cell


You need to put your button in the last cell of UITableView for that you can create a custom Cell like below.

Goto File->New->Cocoa Touch Class then name yourCell and create Xib

enter image description here

Now click on newaly created Xib and design your cell. In your case you can put a button in the cell and apply some autoLayout Constraints as follows

enter image description here

Now create an IBOutlet of your button in your custom cell class. you custom cell class Should look something like this.

import UIKit

class YourTableViewCell: UITableViewCell {

@IBOutlet weak var yourButton: UIButton!

override func awakeFromNib() {
    // Initialization code

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourTableViewCell {

    let kYourTableViewCellIdentifier = "kYourTableViewCellIdentifier"
    tableView.registerNib(UINib(nibName: "YourTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCellWithIdentifier(kYourTableViewCellIdentifier, forIndexPath: indexPath) as! YourTableViewCell

    return cell


Your custom cell is ready to use now. So in your tableViewController class cellForRowAtIndexPath writhe this code

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

if (indexPath.row == (arrReceipt.count)) {

    let cell = YourTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
    // code for last cell 
    return cell

} else {
    let cell:PrintCell = (tblReceipt.dequeueReusableCellWithIdentifier("ButtonCell"))! as! PrintCell

    // code for other rows in tableView
    return cell

and in numberOfRowsInSection return yourDataSourceArray.count + 1

I Hope this will help. If you find any difficulty let me know.