I have a tableview whose height is dynamically adjusted. The height is adjusted so that the tableview only includes a certain number of rows, and no empty rows. So, if there are only 2 rows then the height is smaller, and if there are 4 rows then the height is bigger. However, I do not want its height to go past a certain point. How can I allow the height to be dynamically adjusted, while not letting the height ever reach past a certain point?
This is my code:
#IBOutlet var tableView: UITableView!
#IBOutlet var tableViewHeightConstraint: NSLayoutConstraint!
var items: [String] = ["Swift", "Is", "So", "Amazing"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell
// Make sure the table view cell separator spans the whole width
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
override func viewWillAppear(_ animated: Bool) {
// Adjust the height of the tableview
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
// Add a border to the tableView
tableView.layer.borderWidth = 1
tableView.layer.borderColor = UIColor.black.cgColor
print("We ran ViewWillAppear")
}
// This function is used for adjusting the height of the tableview
override func viewDidLayoutSubviews(){
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
tableView.reloadData()
}
// Allow cell deletion in tableview
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// delete item at indexPath
self.items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
self.tableViewHeightConstraint.constant = self.tableView.contentSize.height - 44
print(self.items)
print("Number of rows: \(tableView.numberOfRows(inSection: 0))")
}
delete.backgroundColor = UIColor.blue
return [delete]
}
In viewWillAppear , you can try
let limit:CGFloat = 900.0
if tableView.contentSize.height < limit {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: tableView.contentSize.height)
}
else {
tableView.frame = CGRect(x: tableView.frame.origin.x, y: tableView.frame.origin.y, width: tableView.frame.size.width, height: limit )
}
Or set value to
tableViewHeightConstraint.constant = ( tableView.contentSize.height < limit ) ? tableView.contentSize.height : limit
self.view.layoutIfNeeded()
Related
I'm creating a bubble chat like application. I created custom functions for the receiving and sending bubble chats.
After creating them, I've added them into the cells that I've created using the UITableView.
However, I am not able to set the height of the cell, depending on the height of the custom cell itself.
For example, some text passed in could be longer than the one before.
Thus I can't seem to set each cell according to the custom bubble chat height.
Please help!
This are my tableview functions
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrMsg.count
}
// second
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
// lastly created
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath)
if arrMsg[indexPath.row]["type"] as! String == "outgoing" {
cell.contentView.addSubview(createOutGoingBubble(msg: arrMsg[indexPath.row]["message"] as! String))
}
if arrMsg[indexPath.row]["type"] as! String == "incoming" {
cell.contentView.addSubview(createIncomingBubble(mymsg: arrMsg[indexPath.row]["message"] as! String))
}
return cell
}
This is how it looks currently
How do I set the height of the cell according to the custom bubble created inside the heightforrowat function?
Probably using the line to find the height and return it:
arrMsg[indexPath.row]["message"] as! String == "outgoing"
**This is my function to create the custom bubble chats to be added into the tableviewcell
func createOutGoingBubble(msg:String) -> UIView {
let subV = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
let label = UILabel()
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 18)
label.textColor = .white
label.text = msg
let constraintRect = CGSize(width: 0.66 * view.frame.width,
height: .greatestFiniteMagnitude)
let boundingBox = msg.boundingRect(with: constraintRect,
options: .usesLineFragmentOrigin,
attributes: [.font: label.font],
context: nil)
label.frame.size = CGSize(width: ceil(boundingBox.width),
height: ceil(boundingBox.height))
let bubbleSize = CGSize(width: label.frame.width + 28,
height: label.frame.height + 20)
let outgoingMessageView = UIImageView(frame:
CGRect(x: view.frame.width - bubbleSize.width - 20,
y: bubbleSize.height - 25,
width: bubbleSize.width,
height: bubbleSize.height))
let bubbleImage = UIImage(named: "outgoing")?
.resizableImage(withCapInsets: UIEdgeInsets(top: 17, left: 21, bottom: 17, right: 21),
resizingMode: .stretch)
.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
outgoingMessageView.image = bubbleImage
outgoingMessageView.tintColor = UIColor(red: 0.40 , green: 0.71, blue: 1.00, alpha: 1)
subV.addSubview(outgoingMessageView)
label.center = outgoingMessageView.center
subV.addSubview(label)
return subV
}
Thanks in advance!
You can use Autolayout for the same and get free from all the coding part. Just set the constraint of the inner view in association with the cell and then right the following code.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:ChatSenderTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "ChatSenderTableViewCell", for: indexPath) as? ChatSenderTableViewCell)!
cell.LabelChatMessage.text = "Your Message"
cell.LabelChatMessage.numberOfLines = 0
cell.LabelChatMessage.sizeToFit()
return cell
}
I am using a custom cell name ChatSenderTableViewCell to create a bubble like effect containing a label for messages send or receive.
Use label inside of view and use margin from top and bottom according to your desire. Also, you need to make 0 number of lines in UIlabel. you can do it by using label.numberoflines = 0.
Also, you need to add the following code to change the height of the row according to the text inside it.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You can try this:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
guard let cell = tableView.cellForRow(at: indexPath) else { return 0 }
return cell.intrinsicContentSize.height
}
The intrinsicContentSize is the natural size for the receiving view, considering only properties of the view itself.
I have a cell in a tableview that contains a horizontal paging collection view.
Inside each page of this collection view there is a vertical collection view.
In order to avoid a "scroll-in-scroll" problem, I disabled vertical scrolling in the vertical collection views.
The vertical collection view's cell count is not static and can be any number.
So this creates a problem: the collection view (you'll have to ask me for clarification about which one) won't fit properly into the table view's cell anymore.
To solve this, I changed the table view's cell height by changing the constraints in the storyboard... but the problem remains.
Here is an image to help illustrate the problem:
and here is my code:
class myTBViewController: UITableViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 10 {
return 2
} else {
return 30
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 10 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mainPages", for: indexPath) as! mainPages
cell.backgroundColor = UIColor.yellow
cell.listCV.clipsToBounds = true
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "showAdvCell", for: indexPath) as! showAdvCell
cell.backgroundColor = UIColor.red
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if collectionView.tag == 10 {
let collectionViewHeight = collectionView.frame.height
let itemsHeight = collectionView.contentSize.height
let topInset = ( collectionViewHeight - itemsHeight ) / 4
return UIEdgeInsetsMake(topInset ,0, 0 , 0)
} else {
let collectionViewHeight = collectionView.frame.height
let itemsHeight = CGFloat(80)
let topInset = ( collectionViewHeight - itemsHeight ) / 4
return UIEdgeInsetsMake(topInset ,0, 0 , 0)
}
}
var headerSegment = UISegmentedControl()
override func viewDidLoad() {
super.viewDidLoad()
headerSegment.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 30)
let items = ["فروشگاه ها", "دیوار"]
headerSegment = UISegmentedControl(items: items)
headerSegment.selectedSegmentIndex = 0
headerSegment.addTarget(self, action: #selector(selectPage), for: UIControlEvents.valueChanged)
let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 200)
let headerImageView = UIImageView(frame: frame)
let image: UIImage = UIImage(named: "1")!
headerImageView.image = image
let frame3 = CGRect(x: 0, y: 0, width: 80, height: 80)
let headerImageView2 = UIImageView(frame: frame3)
let image2: UIImage = UIImage(named: "1")!
headerImageView2.image = image2
headerSegment.addSubview(headerImageView2)
headerSegment.bringSubview(toFront: headerImageView2)
headerImageView2.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 200)
headerSegment.frame = CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width + 10 , height: 40)
headerSegment.center = CGPoint(x: UIScreen.main.bounds.width / 2, y: 200)
let mainView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 230))
let headerSegmentView = UIView(frame : CGRect(x: 0, y: 200, width: UIScreen.main.bounds.width, height: 40))
headerSegmentView.center = headerSegment.center
headerSegmentView.backgroundColor = UIColor.white
mainView.addSubview(headerImageView)
mainView.bringSubview(toFront: headerImageView)
mainView.addSubview(headerSegmentView)
mainView.bringSubview(toFront: headerSegmentView)
mainView.addSubview(headerSegment)
mainView.bringSubview(toFront: headerSegment)
mainView.addSubview(headerImageView2)
mainView.bringSubview(toFront: headerImageView2)
self.tableView.tableHeaderView = mainView
headerImageView2.layer.borderWidth = 2
headerImageView2.layer.masksToBounds = false
headerImageView2.layer.borderColor = UIColor.init(white: 255/255, alpha: 0.3).cgColor
headerImageView2.layer.cornerRadius = headerImageView2.frame.height/2
headerImageView2.clipsToBounds = true
}
#objc func selectPage() {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "myTBViewCell") as! myTBViewCell
switch headerSegment.selectedSegmentIndex {
case 0:
print("0")
cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .top, animated: false)
case 1:
print("1")
cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 1, section: 0), at: .top, animated: false)
default:
print("0")
cell.myTBCollectionView.scrollToItem(at:IndexPath(item: 0, section: 0), at: .top, animated: false)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
return 600
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "myTBViewCell", for: indexPath) as! myTBViewCell
return cell
}
}
I have a similar Problem , finally, I have solved it by creating a height Constraint of CollectionView.
than setting CollectionView height Constraint based on CollectionView ContentSize.
self.collectionViewHeight.constant = self.recentCollectionView.collectionViewLayout.collectionViewContentSize.height
the alternative approach is to override systemLayoutSizeFitting method of tableViewCell
ex:
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
self.tagCollectionView.layoutIfNeeded()
return self.tagCollectionView.collectionViewLayout.collectionViewContentSize
}
in addition you don't need to return fixed 600 height in your table view delegate heightForRowAtIndexPath
I am developing a custom tableView with stretchy headerView and it also has sticky headerView.
I have successfully achieved it by updating its contentInset and contentOffset properties. But after updating its contentInset the scroll becomes jerky.
Below is the code for the same:
I have added below code in viewDidLoad method of viewController.and called another method updateHeaderView to update headerView Height and y position. And kTableHeaderHeight is the constant tableView header height.
headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: kTableHeaderHeight))
headerView = tableView.tableHeaderView
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsets(top: kTableHeaderHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -kTableHeaderHeight)
In updateHeaderView method I have added code:
var headerRect = CGRect(x: 0, y: -kTableHeaderHeight, width: tableView.bounds.width, height: kTableHeaderHeight)
var insetTop: CGFloat = 0
if tableView.contentOffset.y < -kTableHeaderHeight {
headerRect.origin.y = tableView.contentOffset.y
headerRect.size.height = -tableView.contentOffset.y
insetTop = kTableHeaderHeight
}else if tableView.contentOffset.y >= 0 {
insetTop = 0
}else if tableView.contentOffset.y <= 0{
insetTop = -tableView.contentOffset.y
}
tableView.contentInset = UIEdgeInsets(top: insetTop, left: 0, bottom: 0, right: 0)
tableView.scrollIndicatorInsets = UIEdgeInsets(top: insetTop, left: 0, bottom: 0, right: 0)
headerView.frame = headerRect
And called above method from scrollView's delegate method scrollviewDidScroll.
After doing all this stuff the sticky and stretchy tableView behavior is as expected but the tableView scroll becomes jerky. And just for the information, I don't want to use any third party library.
Other Methods used for the class
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if (indexPath.row % 2) == 0 {
cell.backgroundColor = UIColor.gray
}else {
cell.backgroundColor = UIColor.white
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderViewCell") as! HeaderViewCell
return headerCell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 53.0
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.updateHeaderView()
}
}
i am trying to make space between cells in UItableview i've checked google all posts i found is more than 3 years old , when i try to apply them i am getting so many errors, is it possible to make space between cells in UItableview ?
my Code :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ImageCell") as! ImageCell
cell.model = self.modelAtIndexPath(indexPath)
let url = URL(string: cell.model.imageName)
cell.imgBack.kf.setImage(with: url)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//send back playlist count
if (section == 0) {
return self.lists_arr.count
}
return 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated:true)
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let imageCell = cell as! ImageCell
self.setCellImageOffset(imageCell, indexPath: indexPath)
}
//number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func modelAtIndexPath(_ indexPath: IndexPath) -> CellPlaylist {
return self.lists_arr[(indexPath as NSIndexPath).row % self.lists_arr.count]
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView == self.tblMain) {
for indexPath in self.tblMain.indexPathsForVisibleRows! {
if let imgCel : ImageCell = self.tblMain.cellForRow(at: indexPath) as? ImageCell {
self.setCellImageOffset(imgCel, indexPath: indexPath)
}
}
}
}
func setCellImageOffset(_ cell: ImageCell, indexPath: IndexPath) {
let cellFrame = self.tblMain.rectForRow(at: indexPath)
let cellFrameInTable = self.tblMain.convert(cellFrame, to:self.tblMain.superview)
let cellOffset = cellFrameInTable.origin.y + cellFrameInTable.size.height
let tableHeight = self.tblMain.bounds.size.height + cellFrameInTable.size.height
let cellOffsetFactor = cellOffset / tableHeight
cell.setBackgroundOffset(cellOffsetFactor)
}
The class of TableCell :
class ImageCell: UITableViewCell {
#IBOutlet weak var lblTitle: UILabel!
#IBOutlet weak var imgBack: UIImageView!
#IBOutlet weak var imgBackTopConstraint: NSLayoutConstraint!
#IBOutlet weak var imgBackBottomConstraint: NSLayoutConstraint!
let imageParallaxFactor: CGFloat = 70
var imgBackTopInitial: CGFloat!
var imgBackBottomInitial: CGFloat!
var model: CellPlaylist! {
didSet {
self.updateView()
}
}
override func awakeFromNib() {
self.clipsToBounds = true
self.imgBackBottomConstraint.constant -= 2 * imageParallaxFactor
self.imgBackTopInitial = self.imgBackTopConstraint.constant
self.imgBackBottomInitial = self.imgBackBottomConstraint.constant
}
func updateView() {
// self.imgBack.imageFromServerURL(urlString: self.model.imageName)
//self.getImage(url: self.model.imageName,imgView: self.imgBack)
//self.model.imageName
self.layer.cornerRadius = 10
self.layer.masksToBounds = true
self.layer.cornerRadius = 8
self.layer.shadowOffset = CGSize(width: 0, height: 3)
self.layer.shadowRadius = 3
self.layer.shadowOpacity = 0.3
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 8, height: 8)).cgPath
self.layer.shouldRasterize = true
self.layer.rasterizationScale = UIScreen.main.scale
self.contentView.layoutMargins.top = 20
self.lblTitle.text = self.model.title
}
func setBackgroundOffset(_ offset:CGFloat) {
let boundOffset = max(0, min(1, offset))
let pixelOffset = (1-boundOffset)*2*imageParallaxFactor
self.imgBackTopConstraint.constant = self.imgBackTopInitial - pixelOffset
self.imgBackBottomConstraint.constant = self.imgBackBottomInitial + pixelOffset
}
}
The storyboard :
what i am getting
:
what i want :
Make Tablview BG colour white and set BG View colour in cell that you want to keep see in image i have set bgview BG colour is light greay and make BGview hight smaller then cell i mean if you want to set space 10 pixel keep Bg hight smaller then cell 10 pixel
Here is my cell
And here is out put
Update: UIEdgeInsetsInsetRect method is replaced now you can do it like this
contentView.frame = contentView.frame.inset(by: margins)
Swift 4 answer:
in your custom cell class add this function
override func layoutSubviews() {
super.layoutSubviews()
//set the values for top,left,bottom,right margins
let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, margins)
}
You can change values as per your need
Swift 4.2 solution
Inside UITableViewCell subclass
override func layoutSubviews() {
super.layoutSubviews()
let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
bounds = bounds.inset(by: padding)
}
Swift 5.0 solution
Inside UITableViewCell subclass
override func layoutSubviews() {
super.layoutSubviews()
let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
bounds = UIEdgeInsetsInsetRect(bounds, padding)
}
You can let every cell to take a section in group tableView, then you set the section's footer.
Try my code:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.register(UINib.init(nibName: "VC1Cell", bundle: nil), forCellReuseIdentifier: "VC1Cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: -- tableview delegate
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view:UIView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: self.view.bounds.size.width, height: 10))
view.backgroundColor = .clear
return view
}
func numberOfSections(in tableView: UITableView) -> Int {
return 10
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:VC1Cell = tableView.dequeueReusableCell(withIdentifier: "VC1Cell") as! VC1Cell
return cell
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10.0
}
}
The result:
Building on Gulfam Khan's answer, the new way of setting the inset:
Swift 4:
override func layoutSubviews() {
super.layoutSubviews()
//set the values for top,left,bottom,right margins
let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
contentView.frame = contentView.frame.inset(by: margins)
}
Swift 4:
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0))
}
Swift 5.2 Solution
override func layoutSubviews() {
super.layoutSubviews()
let padding = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
bounds = bounds.inset(by: padding)
}
I set up my tableView and has two springVar as shown in the image below. I want my tableview to show empty cells after the two springVar so that my solve bar is at the bottom of the screen. Any advices on how to go about it? My code is below.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return springVar.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
}
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
var data : NSManagedObject = springVar[indexPath.row] as! NSManagedObject
cell!.textLabel!.text = "Spring \(indexPath.row + 1)"
var force : Float = data.valueForKey("force") as! Float
var stiffness : Float = data.valueForKey("stiffness") as! Float
cell!.detailTextLabel!.text = "Force =\(force) \t Stiffness =\(stiffness)"
cell!.detailTextLabel!.textColor = UIColor .darkGrayColor()
return cell!
}
i think this maybe helpfull
if indexPAth.row / 2 == 0
{
return UITableViewCell()
// u can customize ur cell to show a button or anything else
}
this show a custom/blank cell after each 2 cell,(odd)
You can add Footer view in your tableView this way:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
footerView.backgroundColor = UIColor.whiteColor()
//add button in your footer view
var button = UIButton(frame: CGRectMake(0, 0, 75, 30))
button.setTitle("Solve", forState: UIControlState.Normal)
button.setTitleColor(UIColor.brownColor(), forState: UIControlState.Normal)
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
footerView.addSubview(button)
return footerView
}
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 40.0
}
And your result will be:
You can adjust the footerHeight according to your own needs.
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footerHeight : CGFloat = (tableView.frame.size.height - (CGFloat(springVar.count) * 45.0) - 45.0 - 64.0)
let oneCell = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: footerHeight))
return oneCell
}
override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let footerHeight : CGFloat = (tableView.frame.size.height - (CGFloat(springVar.count) * 45.0) - 45.0 - 64.0)
return footerHeight
}